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


当前回答

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

其他回答

标准“瓦尼拉”JavaScript如何重新引导页面

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

location.href = 'newPage.html';


以下部分适用于使用 HTTP_REFERER 作为许多安全措施之一(尽管这不是一个很好的保护措施)。如果您使用 Internet Explorer 8 或更低,这些变量在使用任何形式的 JavaScript 页面重定向(location.href 等)时会失去。

下面我们将实施一个替代 IE8 和更低,以免失去 HTTP_REFERER. 否则,你几乎总是可以简单地使用 window.location.href。

对 HTTP_REFERER 进行测试(URL 粘贴、会话等)可以帮助您知道请求是否合法(注:在评论中提到 drop 的链接中,还有一些方法可以工作周围 / 扫描这些引用器)


简单的跨浏览器测试解决方案(下载到 window.location.href for Internet Explorer 9+ 和所有其他浏览器)

使用:重定向(“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; 
    }
}

此分類上一篇: https://jquery.com/browser-support/

<!DOCTYPE html>
<html>
    <head>
        <title>example</title>
    </head>
    <body>
        <p>You will be redirected to google shortly.</p>
        <script>
            setTimeout(function(){
                window.location.href="http://www.google.com"; // The URL that will be redirected too.
            }, 3000); // The bigger the number the longer the delay.
        </script>
    </body>
</html>

不同选项如下:

window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL

window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"

您也可以使用 meta 数据进行页面重定向如下。

梅塔新鲜

<meta http-equiv="refresh" content="0;url=http://evil.example/" />

地图位置

<meta http-equiv="location" content="URL=http://evil.example" />

<base href="http://evil.example/" />

在此页面上可以找到许多其他方法将您的无疑客户端重新引导到他们可能不想去的页面(其中没有一个依赖于jQuery):

https://code.google.com/p/html5security/wiki/Redirection方法

您也可能被报告为恶意网站. 如果发生的话,当人们点击到您的网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。

<!DOCTYPE html>
<html>
    <head>
        <title>Go Away</title>
    </head>
    <body>
        <h1>Go Away</h1>
        <script>
            setTimeout(function(){
                window.history.back();
            }, 3000);
        </script>
    </body>
</html>

如果你将两页的例子结合在一起,你会有一个婴儿路由,这将保证你的用户永远不会想再使用你的网站。

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

这就是我如何使用它。

   window.location.replace('yourPage.aspx');   
   // If you're on root and redirection page is also on the root

   window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');

   // If you're in sub directory and redirection page is also in some other sub directory.

ECMAScript 6 + jQuery,85位元

$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")

请不要杀了我,这是一个笑话,这是一个笑话,这是一个笑话。

这“提供了一个答案”,在这个意义上,它要求一个解决方案“使用jQuery”,这在这种情况下意味着以某种方式强迫它进入方程式。

Ferrybig 显然需要解释的笑话(仍然开玩笑,我确信在评论表格上有有限的选项),所以没有进一步的 ado:

其他答案是使用 jQuery 的 attr() 在位置或窗口对象不必要。

这个答案也滥用它,但更荒谬的方式. 而不是使用它来设置位置,它使用 attr() 恢复一个函数,设置位置。

“85比特”是指代码高尔夫,高尔夫显然不是你应该在代码高尔夫以外做的事情,而且这个答案显然不是真正的高尔夫。

基本上是克里斯蒂安。