【问题标题】:Get current URL and modify subdirectory and then go to URL with Javascript获取当前 URL 并修改子目录,然后使用 Javascript 转到 URL
【发布时间】:2012-03-29 12:07:54
【问题描述】:

我正在为客户创建一个双语网站。将创建两个不同语言版本的站点并将其存储在两个文件夹中: /zh/ /chi/

我想要做的是创建一个链接以在两种语言之间切换。在概念层面上,我理解 Javascript 可以检测当前 URL 并将其拆分为不同的组件,修改其中的部分(在这种情况下在 /en/ 和 /chi/ 之间更改),然后在链接被点击。

但我对 javascript 的了解为零,所以我不知道如何执行......我遇到了这个页面: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ 但它没有解释如何修改并转到新链接。

您的帮助将不胜感激!

【问题讨论】:

    标签: javascript window.location geturl


    【解决方案1】:

    为了不破坏可用性考虑,如 Shift + 单击 在新窗口中打开,您应该创建一个指向其他语言 URL 的普通旧链接 (<a>)。通过 JavaScript 构建链接没有任何问题,但您也可以在服务器上使用 PHP 或您使用的任何模板语言来完成。

    如果您决定要这样做,这里有一个使用 JavaScript 执行此操作的脚本。

    <!DOCTYPE html>
    <body>
      Content before the link.
      <script>
        (function () {
          // this assumes you're on the en version and want to switch to chi
          var holder = document.createElement("div");
          var url = window.location.href.replace("/en/", "/chi/");
          var link = document.createElement("a");
    
          link.innerText = "Chewa"; // or whatever the link should be
          link.href = url;
          holder.appendChild(link);
          document.write(holder.innerHTML);
        })();
      </script>
      Content after the link.
    </body>
    

    【讨论】:

    • 很高兴它为您服务。如果你喜欢这里的任何答案,你应该接受它。否则,请删除一些 cmets,让我们知道缺少什么。
    【解决方案2】:

    如果您只是想获取完整的 URL 并将 /en/ 替换为 /chi/ 或反之亦然,请使用以下代码。

    HTML

    <span onclick="SwitchLang()">View [Some other Language]</span>
    

    JavaScript

    function SwitchLang() {
        //Does URL contain "/en/"?
        if(window.location.href.indexOf("/en/") != -1) {
            //URL contain "/en/", replace with "/chi/"
            window.location.href = window.location.href.replace("/en/", "/chi/");
        }
         //Does URL contain "/chi/"?
        else if(window.location.href.indexOf("/chi/") != -1) {
            //URL contain "/chi/", replace with "/en/"
            window.location.href = window.location.href.replace("/chi/", "/en/");
        }
    }
    

    或者,更简洁一点(未注释的版本)

    function SwitchLang() {
        if(window.location.href.indexOf("/en/") != -1)
            window.location.href = window.location.href.replace("/en/", "/chi/");
        else if(window.location.href.indexOf("/chi/") != -1)
            window.location.href = window.location.href.replace("/chi/", "/en/");
    }
    

    注意:在JS中,当你修改window.location.href时,会自动加载新的URL。

    这里有a working fiddle 供你玩。

    【讨论】:

    • 这是一个干净且有效的解决方案(如小提琴所示)。为什么要打-1?
    【解决方案3】:

    您似乎需要更改window.location.pathname。例如:

    // assuming the url `http://www.example.org/en/foo/bar/page.html`
    
    var paths = window.location.pathname.split("/");
    // change `en`
    paths[1] = "chi";
    // go to the new url
    window.location.pathname = paths.join("/");
    

    见:

    https://developer.mozilla.org/en/DOM/window.location

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2018-02-04
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多