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


当前回答

这就是我如何使用它。

   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.

其他回答

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

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

首先写得正确. 你想在一个应用程序内导航另一个链接从你的应用程序另一个链接. 这里是代码:

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

如果你想在你的应用程序内浏览页面,那么我也有代码,如果你愿意。

ECMAScript 6 + jQuery,85位元

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

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

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

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

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

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

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

基本上是克里斯蒂安。

如果您更喜欢使用纯粹的JavaScript,我意识到使用document.location.href = “https://example.com” 或 window.location.href = “https://example.com” 会导致 Firefox 中的兼容性问题。

location.href = "https://example.com";
location.replace("http://example.com");

在我看来,这个问题已经解决了,祝你好运!

与 jQuery 合作:

$(window).attr("location", "http://google.fr");