使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
使用 :
function redirect(a) {
location = a
}
并称之为:redirect([url]);
没必要href
之后location
,因为它是隐含的。
其他回答
标准“ vanilla” JavaScript 重定向页面的方法
window.location.href = 'newPage.html';
或更简单: (自window
全球)
location.href = 'newPage.html';
如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :
(否则忽略此最后部分)
下一节针对的是那些使用HTTP_REFERER
作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。
我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href
.
测试HTTP_REFERER
(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)
简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)
用法 :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;
}
}
这就是我如何使用它。
window.location.replace('yourPage.aspx');
// If you're on root and redirection page is also on the root
window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');
// If you're in sub directory and redirection page is also in some other sub directory.
我也认为location.replace(URL)
这是最好的方法, 但是如果您想要通知搜索引擎您的调整方向( 他们不分析 JavaScript 代码来查看调整方向) , 您应该添加rel="canonical"
网站的元标签 。
添加带有 HTML 刷新元标记的标注部分, 也是一个很好的解决方案 。 我建议您使用此JavaScript 调整方向工具以创建再定向。它也拥有互联网探索者支持以通过 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.example/"/>
<noscript>
<meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://yourdomain.example/";
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 -->
所以,问题在于如何改变页面方向, 而不是如何改变网页方向?
您只需要为此使用 JavaScript 即可。 这里有一些小代码, 可以创建一个动态重定向页面 。
<script>
var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
if( url ) window.location.replace(url);
</script>
所以说,你只是把这个片段redirect/index.html
在您网站上的文档中,您可以这样使用它 。
http://www.mywebsite.com/redirect?url=http://stackoverflow.com
如果你去那个链接 它会自动引导你到堆叠流. com.
这就是你如何制造简单使用 JavaScript 重定向页面
编辑 :
还有一件事需要指出。window.location.replace
在我的代码中,因为我认为它适合 重置页面, 但是,你必须知道,当使用window.location.replace
当您在浏览器中按回按钮时,它会被重定向否回到重定向页面, 它会回到之前的页面, 看看这个小演示的东西。
示例:
进程:家居存储店 => 将页面重定向到谷歌 => 谷格
当谷歌:谷格 => 在浏览器中的后退按钮 => 家居存储店
因此,如果这适合您的需求, 那么一切都应该很好。 如果您想要在浏览器历史中包含重置页面, 替换此
if( url ) window.location.replace(url);
与
if( url ) window.location.href = url;
每个浏览器都使用此功能 :
window.location.href = 'your_url';
推荐文章
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 在JavaScript关联数组中动态创建键
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?