我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
在我的工作经验中,JavaScript更好地重定向。
它取决于您想要如何更改位置. 如果您想在用户历史中登录您的网站,请使用 window.location.href='ur website';. 否则,如果您不登录历史,请使用 window.location.replace(“您的网站”);。
其他回答
与 jQuery 合作:
$(window).attr("location", "http://google.fr");
使用 JavaScript:
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 jQuery:
方法1: $(位置)
$(location).attr('href', 'http://google.com');
方法2:可重新使用的功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");
您可以在 jQuery 中以以下方式重定向:
$(location).attr('href', 'http://yourPage.com/');
但是,如果有人想重新引导到主页,那么他可以使用下面的剪辑。
window.location = window.location.host
如果你有三种不同的环境,如发展、排行和生产,这将是有用的。
您可以通过在 Chrome 控制台或 Firebug 的控制台中放置这些单词来探索此窗口或 window.location 对象。
<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>