我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

JavaScript 的内容:

window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');

吉克里:

var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window

其他回答

我只是添加另一种方式:

要将您的网站的任何特定页面/链接转向另一个页面,只需添加此代码行:

<script>
    if(window.location.href == 'old_url')
    {
        window.location.href="new_url";
    }

    // Another URL redirect
    if(window.location.href == 'old_url2')
    {
        window.location.href="new_url2";
    }
</script>

作为现实生活的例子,

<script>
    if(window.location.href == 'https://old-site.com')
    {
        window.location.href="https://new-site.com";
    }

    // Another URL redirect
    if(window.location.href == 'https://old-site.com/simple-post.html')
    {
        window.location.href="https://new-site.com/simple-post.html";
    }
</script>

使用这个简单的代码,您可以重新引导完整的网站或任何单页。

警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。

$(location).prop('href', 'http://stackoverflow.com')

在您的点击功能上,只需添加:

window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
    window.location.href = "http://www.google.com";
});

我只是用另一个更新的jQuery方法更新这个荒谬:

var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);

使用 jQuery/JavaScript 重新引导用户

使用 jQuery 或 JavaScript 的位置对象,我们可以将用户转向另一个网页。

在 jQuery

将用户从一页转向另一页的代码是:

var url = 'http://www.example.com';
$(location).attr('href', url);

在JavaScript

将用户从一页转向另一页的代码是:

var url = 'http://www.example.com';
window.location.href = url;

var url = 'http://www.example.com';
window.location = url;