使用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/")
其他回答
var url = 'asdf.html';
window.location.href = url;
您可以在没有jQuery 的情况下这样做 :
window.location = "http://yourdomain.com";
如果你只想要jQuery, 你可以做它喜欢:
$jq(window).attr("location","http://yourdomain.com");
我们不能简单地使用 jQuery 重置
j/ 查询没有必要,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";
ECMAScript 6 + jQuery,85 字节
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
请不要杀我,这是个玩笑,这是个玩笑这是一个笑话。
这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,
Ferrybig显然需要解释笑话(还是开玩笑,
其他的答案是使用 jQuery 的attr()
会 议 日 程 和 议 程location
或window
不必要的物体。
这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()
获取设置位置的函数。
函数被命名jQueryCode
即使它没有任何jQuery, 并且号召一个函数somethingCode
特别是当事情甚至不是语言的时候。
“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。
基本上, 畏缩。