如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一页面?
#1楼
您可以在没有jQuery的情况下做到这一点:
window.location = "http://yourdomain.com";
如果只需要jQuery,则可以这样做:
$jq(window).attr("location","http://yourdomain.com");
#2楼
重定向页面的标准“香草” JavaScript方法
window.location.href = 'newPage.html';
或更简单地说:(因为window
是“全局”)
location.href = 'newPage.html';
如果您在这里是因为重定向时丢失了 HTTP_REFERER,请继续阅读:
(否则忽略最后一部分)
以下部分适用于那些将HTTP_REFERER
用作许多安全措施之一的人(尽管这不是很好的保护措施)。 如果您使用的是Internet Explorer 8或更低版本,则在使用任何形式的JavaScript页面重定向(location.href等)时,这些变量都会丢失。
下面我们将为IE8及更低版本实现替代方案,以免丢失HTTP_REFERER。 否则,您几乎总是可以简单地使用window.location.href
。
测试对HTTP_REFERER
(URL粘贴,会话等) 可以在讲述一个请求是否合法,有益的。 ( 请注意:还有一些方法可以解决/欺骗这些引荐来源网址,如评论中的下垂链接所示)
简单的跨浏览器测试解决方案(对于Internet Explorer 9+和所有其他浏览器,回退到window.location.href)
用法: redirect('anotherpage.aspx');
function redirect (url) {
var ua = navigator.userAgent.toLowerCase(),
isIE = ua.indexOf('msie') !== -1,
version = parseInt(ua.substr(4, 2), 10);
// Internet Explorer 8 and lower
if (isIE && version < 9) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
}
// All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
else {
window.location.href = url;
}
}
#3楼
但是,如果有人想重定向回主页,则可以使用以下代码段。
window.location = window.location.host
如果您具有三个不同的环境(如开发,登台和生产),这将很有帮助。
您可以通过将这些词放在Chrome控制台或Firebug的控制台中来探索该window或window.location对象。
#4楼
在点击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});
#5楼
var url = 'asdf.html';
window.location.href = url;
#6楼
我也认为location.replace(URL)
是最好的方法,但是如果您想通知搜索引擎您的重定向(他们不分析JavaScript代码来查看重定向),则应添加rel="canonical"
元标记到您的网站。
添加一个带有HTML刷新元标记的noscript部分也是一个很好的解决方案。 我建议您使用此JavaScript重定向工具创建重定向。 它还具有Internet Explorer支持,以传递HTTP引荐来源网址。
没有延迟的示例代码如下所示:
<!-- Place this snippet right after opening the head tag to make it work properly -->
<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.com/"/>
<noscript>
<meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://yourdomain.com/";
if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
{
document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
#7楼
警告:仅作为可能的解决方案提供此答案; 它显然不是最好的解决方案,因为它需要jQuery。 相反,您更喜欢纯JavaScript解决方案。
$(location).attr('href', 'http://stackoverflow.com')
#8楼
首先正确编写。 您要在应用程序中导航至另一个链接,而在应用程序中导航至另一个链接。 这是代码:
window.location.href = "http://www.google.com";
而且,如果您想浏览应用程序中的页面,那么如果需要的话,我也有代码。
#9楼
在JavaScript和jQuery中,我们可以使用以下代码将一个页面重定向到另一页面:
window.location.href="http://google.com";
window.location.replace("page1.html");
#10楼
不需要jQuery。 你可以这样做:
window.open("URL","_self","","")
就这么简单!
发起HTTP请求的最佳方法是使用document.loacation.href.replace('URL')
。