我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
<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>
其他回答
你可以使用:
window.location.replace("http://www.example.com/");
替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。
但是,如果你想要一个与点击链接相同的效果,你应该去:
window.location.href = "http://www.example.com/";
在此情况下,浏览器返回按钮将是活跃的。
警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。
$(location).prop('href', 'http://stackoverflow.com')
在我的工作经验中,JavaScript更好地重定向。
它取决于您想要如何更改位置. 如果您想在用户历史中登录您的网站,请使用 window.location.href='ur website';. 否则,如果您不登录历史,请使用 window.location.replace(“您的网站”);。
在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:
window.location.replace("http://www.test.com");
或
location.replace("http://www.test.com");
或
window.location.href = "http://www.test.com";
使用 jQuery:
$(window).attr("location","http://www.test.com");
在您的点击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});