我如何将用户从一个页面转向另一个页面,使用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";
其他回答
我只是用另一个更新的jQuery方法更新这个荒谬:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);
在JavaScript和jQuery中,我们可以使用以下代码将一页转向另一页:
window.location.href="http://google.com";
window.location.replace("page1.html");
<script type="text/javascript">
var url = "https://yourdomain.com";
// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
// All other browsers
else { window.location.replace(url); }
</script>
如果您更喜欢使用纯粹的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.open("URL","_self","","")
这是那么容易的!
启动 HTTP 请求的最佳方式是使用 document.loacation.href.replace(‘URL’)。