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


当前回答

在 JavaScript 和 jQuery 中, 我们使用以下代码重定向页面 :

window.location.href="http://google.com";
window.location.replace("page1.html");

但您可以在 jQuery 中设定函数, 以重定向页面 :

jQuery.fn.redirect=function(url)
{
    window.location.href=url;
}

调用此函数 :

jQuery(window).redirect("http://stackoverflow.com/")

其他回答

应该能够设定使用window.location.

示例:

window.location = "https://stackoverflow.com/";

以下是关于此议题的过去文章:我该如何转到另一个网页?

如果您想要直接重定向到同一应用程序内的路线, 只需

window.location.pathname = '/examplepath'

这将是前进的道路。

var url = 'asdf.html';
window.location.href = url;

ECMAScript 6 + jQuery,85 字节

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

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

这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,

Ferrybig显然需要解释笑话(还是开玩笑,

其他的答案是使用 jQuery 的attr()会 议 日 程 和 议 程locationwindow不必要的物体。

这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()获取设置位置的函数。

函数被命名jQueryCode即使它没有任何jQuery, 并且号召一个函数somethingCode特别是当事情甚至不是语言的时候。

“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。

基本上, 畏缩。

每个浏览器都使用此功能 :

window.location.href = 'your_url';