使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
在 PHP 、 HTML 或 jQuery 区域之后写入下面的代码。如果在 PHP 或 HTML 区域中间,则使用 < shit> 标记。
location.href = "http://google.com"
其他回答
从客户的侧面调整方向:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript and jQuery example to redirect a page or URL </title>
</head>
<body>
<div id="redirect">
<h2>Redirecting to another page</h2>
</div>
<script src="scripts/jquery-1.6.2.min.js"></script>
<script>
// JavaScript code to redirect a URL
window.location.replace("http://stackoverflow.com");
// window.location.replace('http://code.shouttoday.com');
// Another way to redirect page using JavaScript
// window.location.assign('http://code.shouttoday.com');
// window.location.href = 'http://code.shouttoday.com';
// document.location.href = '/relativePath';
//jQuery code to redirect a page or URL
$(document).ready(function(){
//var url = "http://code.shouttoday.com";
//$(location).attr('href',url);
// $(window).attr('location',url)
//$(location).prop('href', url)
});
</script>
</body>
</html>
在单击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});
标准“ 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 = window.location.host
如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。
您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台
我只是补充另一种方式:
要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:
<script>
if(window.location.href == 'old_url')
{
window.location.href="new_url";
}
// Another URL redirect
if(window.location.href == 'old_url2')
{
window.location.href="new_url2";
}
</script>
以真实生活为例
<script>
if(window.location.href == 'https://old-site.com')
{
window.location.href="https://new-site.com";
}
// Another URL redirect
if(window.location.href == 'https://old-site.com/simple-post.html')
{
window.location.href="https://new-site.com/simple-post.html";
}
</script>
通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 登录后,Laravel重定向回原始目的地
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类