代码之家  ›  专栏  ›  技术社区  ›  Tomas Andrle

Javascript将#anchor重定向到单独的页面

  •  6
  • Tomas Andrle  · 技术社区  · 16 年前

    我有一组指向单个网页的#anchors链接,我想顺利地移动到一个模型,每个链接都有一个单独的网页。我希望使用重定向使旧链接继续工作。

    /all_products#A
    /all_products#B
    /all_products#C
    

    /products/A
    /products/B
    /products/C
    

    我知道服务器在请求中没有收到#anchor名称,但Javascript可能会。

    JQuery很好,反正它已经在网站上使用了。

    5 回复  |  直到 16 年前
        1
  •  12
  •   janpio WillBroadbent    7 年前

    extracting the hash from the url doing a redirect .

    // Closure-wrapped for security.
    (function () {
        var anchorMap = {
            "A": "/products/A",
            "B": "/products/B",
            "C": "/products/C"
        }
        /*
        * Best practice for extracting hashes:
        * https://stackoverflow.com/a/10076097/151365
        */
        var hash = window.location.hash.substring(1);
        if (hash) {
            /*
            * Best practice for javascript redirects: 
            * https://stackoverflow.com/a/506004/151365
            */
            window.location.replace(anchorMap[hash]);
        }
    })();
    
        2
  •  3
  •   James Wheare    16 年前

    将其放在HTML顶部附近 <head> 您可以这样做,以便它可以在下载其余页面资源之前执行:

    <script>
    function checkURL() {
        var old_path = '/all_products';
        if (window.location.pathname != old_path) {
            // Not on an old-style URL
            return false;
        }
        // Some browsers include the hash character in the anchor, strip it out
        var product = window.location.hash.replace(/^#(.*)/, '$1');
        // Redirect to the new-style URL
        var new_path = '/products';
        window.location = new_path + '/' + product;
    }
    checkURL();
    </script>
    

    这将检查当前页面的URL,如果它与旧样式路径匹配,则重定向。

    此代码使用 window.location 对象,其中包含当前URL的所有部分,这些部分已经拆分为组成部分。

    让这个脚本更通用是留给实现者的练习。

        3
  •  2
  •   Gregory    16 年前

    我希望这能有所帮助:)

    var urlSplit = document.URL.split("#");
    if (urlSplit[1]) {
        location.href = "http://www.example.org" + "/" + urlSplit[1];
    }
    else {
        location.href = "http://www.example.org";
    }
    
        4
  •  0
  •   kkyy    16 年前

    使用jquery,只需将href替换为正确的href即可:

    $('a').each(function() {
      this.href = this.href.replace(/all_products#/, 'products/');
    });
    

    或捕获点击并重定向:

    $('a').click(function() {
      window.location = this.href.replace(/all_products#/, 'products/');
      return false;
    });
    
        5
  •  -1
  •   TheVillageIdiot    16 年前

    这可能会有所帮助:

    <a href="/all_products#A" onclick="return redirectMe(this);">A</a>
    <a href="/all_products#B" onclick="return redirectMe(this);">B</a>
    <a href="/all_products#C" onclick="return redirectMe(this);">C</a>
    <a href="/all_products#D" onclick="return redirectMe(this);">D</a>
    <a href="/all_products#E" onclick="return redirectMe(this);">E</a>
    
    <script type="text/javascript">
    function redirectMe(a){
       var aa=a+"";
       window.location=aa.replace(/#/g,"/");
    }
    </script>   
    
    推荐文章