2014-02-12 28 views
0

我想,如果用户正在访问分割窗口位置相应

  1. xxxx.com will need to go to xxxx.com/langchoice. 2. xxxx.com/currentlang/test.php will need to go to xxxx.com/langchoice/test.php 根据郎选择下拉菜单并使用他们当前window.location的

    所以将用户重定向3 xxxx.com/test.php will need to go to xxxx.com/langchoice/test.php

我已经做了1和2,但不是特别满意我编码的方式,考虑到如果更多的话年龄可能会增加,我需要每次都添加一行...可以更好地重写吗?

var s = window.location.href; 

       if (s.indexOf(".php") !=-1) 
       { 

        if (s.indexOf("/en/") !=-1) 
        { 
        var location=window.location.href.replace("/en/","/"+evt.selectedItem+"/"); 
        } 
        else if (s.indexOf("/gr/") !=-1) 
        { 
        var location=window.location.href.replace("/gr/","/"+evt.selectedItem+"/"); 
        } 
        else if (s.indexOf("/it/") !=-1) 
        { 
        var location=window.location.href.replace("/it/","/"+evt.selectedItem+"/"); 
        } 
        else 
        { 

        } 

         window.location.replace(location); 
       } 
       else 
       { 
        var location=window.location.href.replace("#",""); 
        window.location.replace(location+evt.selectedItem); 
       } 
+0

您可能想要试用RegEx。 – malkassem

+1

也许动态地在后端(php)中创建语言选择,每个语言版本的完整URL会自动 – fast

回答

1

这确实让检查,看是否有一种“语言”,但其基本思想,以取代将

这样做有它的许多方面,这是一种方式

var orgPathName = window.location.pathname; 
var newPathName = orgPathName.replace(/^\/[^\/]*/,"/" + evt.selectedItem); 
var newUrl = window.location.href.replace(orgPathName, newPathName); 

现在做检测,你做一个简单的测试

var hasLang = (/^\/(en|gr|in)\//i).test(window.location.pathname); 

这种疼痛是保持语言列表

0

你如何坚持langchoice?你把它存储在cookie中?

我想你基本上是说,用户应该是:

xxxx.com/[langchoice]etc 

在任何时候。

所以,你可以拆分'/',然后,如果存在,检查项目[1]。如果它匹配langchoice cookie,请继续,如果不匹配,请将其交换出去。