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


当前回答

这样做有三个主要途径,

window.location.href='blaah.com';
window.location.assign('blaah.com');

还有...

window.location.replace('blaah.com');

最后一个是最好的, 用于传统的重定向, 因为它不会保存您在搜索历史中被重定向之前的页面。 但是, 如果您只想用 JavaScript 打开一个标签, 您可以使用上述任何选项 。1

爱迪特:window前缀是可选的 。

其他回答

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

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

简在JavaScript 贾斯克里普特,您可以通过下列方式将方向转向特定页面:

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

location.replace("http://www.test.com");

window.location.href = "http://www.test.com";

使用 j 查询 :

$(window).attr("location","http://www.test.com");

如果您想做更多描述的话, 这样做会有所帮助。 如果您试图生成页码数据, 这样做的方法中有一些选项。 您可以为您想要直接进入的每页创建单独的链接 。

<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...

请注意,此示例中的当前页面在代码中和与 CSS 的处理方式不同。

如果您想要通过 AJAX 更改页码数据, 则这里是 jQuery 会出现的位置。 您要做的是将点击处理器添加到与不同页面相对应的每个锁定标记中。 此点击处理器会引用一些 jQuery 代码, 该代码会通过 AJAX 去取取下一页, 并用新数据更新表格 。 下面的例子假设您有一个返回新页面数据的网络服务 。

$(document).ready( function() {
    $('a.pager-link').click( function() {
        var page = $(this).attr('href').split(/\?/)[1];
        $.ajax({
            type: 'POST',
            url: '/path-to-service',
            data: page,
            success: function(content) {
               $('#myTable').html(content);  // replace
            }
        });
        return false; // to stop link
    });
});

所以,问题在于如何改变页面方向, 而不是如何改变网页方向?

您只需要为此使用 JavaScript 即可。 这里有一些小代码, 可以创建一个动态重定向页面 。

<script>
    var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
    if( url ) window.location.replace(url);
</script>

所以说,你只是把这个片段redirect/index.html在您网站上的文档中,您可以这样使用它 。

http://www.mywebsite.com/redirect?url=http://stackoverflow.com

如果你去那个链接 它会自动引导你到堆叠流. com.

链接到文档

这就是你如何制造简单使用 JavaScript 重定向页面

编辑 :

还有一件事需要指出。window.location.replace在我的代码中,因为我认为它适合 重置页面, 但是,你必须知道,当使用window.location.replace当您在浏览器中按回按钮时,它会被重定向回到重定向页面, 它会回到之前的页面, 看看这个小演示的东西。

示例:

进程:家居存储店 => 将页面重定向到谷歌 => 谷格

当谷歌:谷格 => 在浏览器中的后退按钮 => 家居存储店

因此,如果这适合您的需求, 那么一切都应该很好。 如果您想要在浏览器历史中包含重置页面, 替换此

if( url ) window.location.replace(url);

if( url ) window.location.href = url;