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

发布于 2024-08-02 13:52:24 字数 448 浏览 1 评论 0原文

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

旧链接样式:

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

新链接样式:

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

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

是否可以使用 Javascript 自动从 /all_products#A 重定向到 /products/A?

JQuery 就可以,无论如何,网站上都在使用它。

I have a set of links with #anchors pointing to a single webpage and I would like to smoothly move to a model with a separate webpage for each of those links. I want the old links to keep working using a redirect.

Old link style:

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

New link style:

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

I know that the server does not receive the #anchor name in the request but Javascript might.

Is it possible to automatically redirect from /all_products#A to /products/A using Javascript?

JQuery would be fine, it's being used on the site anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳哥哥 2024-08-09 13:52:24

我添加了这个新答案,以包含从网址中提取哈希进行重定向

// 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]);
    }
})();

I added this new answer to include some best practices for both extracting the hash from the url and 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]);
    }
})();
月亮邮递员 2024-08-09 13:52:24

将其尽可能靠近 HTML 的顶部,以便它可以在页面资源的其余部分下载之前执行:

<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 的所有部分,这些部分已被拆分为多个组成部分。

使这个脚本更加通用是留给实施者的练习。

Put this as close to the top of your HTML <head> as you can so that it can execute before the rest of the page resources download:

<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>

This will check the current page URL and redirect if it matches the old-style path.

This code makes use of the window.location object which contains all the parts of the current URL already split up into component parts.

Making this script more generic is left as an exercise for the implementer.

山有枢 2024-08-09 13:52:24

我希望这能有所帮助:)

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

I hope this can help :)

var urlSplit = document.URL.split("#");
if (urlSplit[1]) {
    location.href = "http://www.example.org" + "/" + urlSplit[1];
}
else {
    location.href = "http://www.example.org";
}
漫雪独思 2024-08-09 13:52:24

使用 jquery,要么只需将 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;
});

With jquery, either just replace the href with the correct one:

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

or capture clicks and redirect:

$('a').click(function() {
  window.location = this.href.replace(/all_products#/, 'products/');
  return false;
});
旧人九事 2024-08-09 13:52:24

这可能有帮助:

<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>   

this might help:

<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>   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文