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


当前回答

<script type="text/javascript">
    if(window.location.href === "http://stackoverflow.com") {      
         window.location.replace("https://www.google.co.in/");
       }
</script>

其他回答

您可以在脚本部分的按钮点击事件下写入 Url. 动作 。

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}

您可以使用以下方法重定向页面:

  1. 通过在头部使用元标记 -<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />注意:content="0;... 用于在您需要多少秒后重定向页面

  2. 使用 JavaScript :window.location.href = "http://your-page-url.com";

  3. 使用 jQuery 查询 :$(location).attr('href', 'http://yourPage.com/');

# HTML 页面使用 jQuery/JavaScript 方法重定向

尝试此示例代码 :

function YourJavaScriptFunction()
{
    var i = $('#login').val();
    if (i == 'login')
        window.location = "Login.php";
    else
        window.location = "Logout.php";
}

如果您想要给完整 URL 提供完整 URLwindow.location = "www.google.co.in";.

标准“ vanilla” JavaScript 重定向页面的方法

window.location.href = 'newPage.html';

或更简单: (自window全球)

location.href = 'newPage.html';

如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :

(否则忽略此最后部分)


下一节针对的是那些使用HTTP_REFERER作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。

我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href.

测试HTTP_REFERER(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)


简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)

用法 :redirect('anotherpage.aspx');

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
    }

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.href = url; 
    }
}

我也认为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 -->