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


当前回答

警告: 警告:答案只是作为一种可能的解决办法而已;显然最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。

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

其他回答

如果您想要直接重定向到同一应用程序内的路线, 只需

window.location.pathname = '/examplepath'

这将是前进的道路。

<script type="text/javascript">
var url = "https://yourdomain.com";

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
    var referLink = document.createElement("a");
    referLink.href = url;
    document.body.appendChild(referLink);
    referLink.click();
}

// All other browsers
else { window.location.replace(url); }
</script>

基本上jj 查询只是一个JavaScript 贾斯克里普特和做一些诸如转向在此情况下, 您可以使用纯 JavaScript, 这样您就有三个选择, 使用 vanilla JavaScript :

(1) 使用地点替换,这将取代页面的当前历史,这意味着无法使用后退按钮返回原始页面。

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

2) 使用地点指派,这将保留您的历史,如果使用后按钮,您可以回到原始页面:

window.location.assign("http://stackoverflow.com");

3) 我建议使用其中一种方法, 但这可能是使用纯JavaScript的第三个选择:

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

您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以使用所有上面没有窗口的函数, 如果您已经进入窗口范围, 例如window.location.replace("http://stackoverflow.com");可以是location.replace("http://stackoverflow.com");

我还在下面的图像上展示了所有这些照片:

location.replace location.assign

使用 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");

我也认为location.replace(URL)这是最好的方法, 但是如果您想要通知搜索引擎您的调整方向( 他们不分析 JavaScript 代码来查看调整方向) , 您应该添加rel="canonical"网站的元标签 。

添加带有 HTML 刷新元标记的标注部分, 也是一个很好的解决方案 。 我建议您使用此JavaScript 调整方向工具以创建再定向。它也拥有互联网探索者支持以通过 HTTP 查询器 。

毫不延迟的样本代码看起来是这样的:

<!-- Place this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.example/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.example/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->