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


当前回答

一个不只是使用jQuery重定向

jQuery 不需要, window.location.replace(...) 最好是模拟一个 HTTP 重定向。

window.location.replace(...) 比使用 window.location.href 更好,因为 replace() 不保留起源页面在会话历史中,这意味着用户不会陷入无止境后按钮故障。

如果你想模拟某人点击链接,使用 location.href

如果您想模拟一个 HTTP 重定向,请使用 location.replace。

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

其他回答

在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:

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

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

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

使用 jQuery:

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

location.assign(): 指定路径通过路径到它.. 指定将给你一个历史,即使路径被分配后. 使用方法:值应该转移到它. 例如: location.assign(“http://google.com”) location.href 可以定义给一个路径到它... 它将重定向到一个特定的路径一旦它设置,它将保持历史... 使用方法:值应该分配到它


assign() 和 href 是相似的,两者都可以持有历史。 assign 将通过通过值工作, href 通过分配工作。

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

你可以很容易地理解两者之间的差异。

这里是位置对象,

此分類上一篇

一个不只是使用jQuery重定向

jQuery 不需要, window.location.replace(...) 最好是模拟一个 HTTP 重定向。

window.location.replace(...) 比使用 window.location.href 更好,因为 replace() 不保留起源页面在会话历史中,这意味着用户不会陷入无止境后按钮故障。

如果你想模拟某人点击链接,使用 location.href

如果您想模拟一个 HTTP 重定向,请使用 location.replace。

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

在 PHP、HTML 或 jQuery 部分后写下下面的代码,如果在 PHP 或 HTML 部分中间,则使用 <script> 标签。

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