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


当前回答

尝试此 :

location.assign("http://www.google.com");

示例代码片断.

其他回答

从客户的侧面调整方向:

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript and jQuery example to redirect a page or URL </title>
    </head>
    <body>
        <div id="redirect">
            <h2>Redirecting to another page</h2>
        </div>

        <script src="scripts/jquery-1.6.2.min.js"></script>
        <script>
            // JavaScript code to redirect a URL
            window.location.replace("http://stackoverflow.com");
            // window.location.replace('http://code.shouttoday.com');

            // Another way to redirect page using JavaScript

            // window.location.assign('http://code.shouttoday.com');
            // window.location.href = 'http://code.shouttoday.com';
            // document.location.href = '/relativePath';

            //jQuery code to redirect a page or URL
            $(document).ready(function(){
                //var url = "http://code.shouttoday.com";
                //$(location).attr('href',url);
                // $(window).attr('location',url)
                //$(location).prop('href', url)
            });
        </script>
    </body>
</html>

标准“ 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; 
    }
}

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

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}
  1. 指定位置( ) :

    将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。

    用法:数值应传递到它中 。

    例如: location.assign("http://google.com")

    Enter image description here

  2. 位置.href

    定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...

    用法:值应该被指定到它中 。

    例如: location.href = "http://google.com"

  3. 位置. replace () :

    如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。

    用法:数值应传递到它中。

    例如: location.replace("http://google.com")

    Enter image description here


assign()href两者相似,可以保留历史。assign通过分配,通过传递一个价值和粗略的作品来工作。

您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :

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

使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,

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

你可以很容易地理解 两者之间的区别...

这里是定位对象,

Location API from chrome

我只是补充另一种方式:

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

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

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