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


当前回答

JavaScript 是非常广泛的. 如果你想跳到另一个页面,你有三个选项。

 window.location.href='otherpage.com';
 window.location.assign('otherpage.com');
 //and...

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

当你想移动到另一个页面,你可以使用其中一个,如果这是你的要求. 但是,所有三个选项都限于不同的情况。

如果你有兴趣了解更多关于这个概念,你可以通过更多。

window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.

其他回答

如何从客户端侧进行重定向:

<!DOCTYPE html> <html> <head> <title>JavaScript and jQuery example to redirect a page or URL </title> </head> <body> <div id="redirect"> <h2>Redirect to another page</h2> </div> <script src="scripts/jquery-1.6.2.min.js"></script> <script> // JavaScript code to redirect a URL window.location.r

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 中以以下方式重定向:

$(location).attr('href', 'http://yourPage.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");

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