使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?


当前回答

与 jQuery 合作 :

$(window).attr("location", "http://google.fr");

其他回答

根据我的工作经验 JavaScript 更适合重新定位

它取决于您要如何更改地址。 如果您想要在用户历史中登录您的网站, 请使用window.location.href='ur website';否则在不记录历史的情况下使用window.location.replace("your website");.

使用 JavaScript :

方法1:

window.location.href="http://google.com";

方法2:

window.location.replace("http://google.com");

使用 j 查询 :

方法1:美元(地点)

$(location).attr('href', 'http://google.com');

方法2:可再使用功能

jQuery.fn.redirectTo = function(url){
    window.location.href = url;
}

jQuery(window).redirectTo("http://google.com");

我只是补充另一种方式:

要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:

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

通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。

var url = 'asdf.html';
window.location.href = url;

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。