使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
我只是补充另一种方式:
要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:
<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>
通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。
其他回答
在我开始之前, jQuery 是用于 DOM 操作的 JavaScript 库。 所以您不应该使用 jQuery 重定向页面 。
Jquery.com的一段引文:
虽然jQuery在旧浏览器版本中可能运行没有大问题,但我们并不积极测试jQuery在其中的位置,而且通常不修补其中可能出现的错误。
发现于此:https://jquery.com/browser-support/
所以jQuery并不是一个终极和万能的解决方案 解决逆向兼容性。
以下使用原始 JavaScript 的解决方案在所有浏览器中都起作用, 并且已经相当长的时间达到了标准, 所以您不需要任何库来支持交叉浏览器 。
此页面将重定向到谷歌谷歌3000毫秒后
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<p>You will be redirected to google shortly.</p>
<script>
setTimeout(function(){
window.location.href="http://www.google.com"; // The URL that will be redirected too.
}, 3000); // The bigger the number the longer the delay.
</script>
</body>
</html>
不同的备选办法如下:
window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL
window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"
使用替换时, 后端按钮将不再返回重置页面, 仿佛它从来没有在历史中出现过。 如果您想要用户能够返回重置页面, 请使用window.location.href
或window.location.assign
。如果您确实使用让用户返回重定向页面的选项,请记住,当您输入重定向页面时,它会将您重新定向。因此,在选择重定向选项时会考虑到这一点。如果该页面仅在用户采取行动时才被重定向,那么在后按钮历史中拥有页面就没事了。但如果页面自动重定向,那么您应该使用替换,这样用户就可以使用后端按钮,而不必被迫返回重定向发送的页面。
您也可以使用元数据运行页面重定向 。
META 刷新
<meta http-equiv="refresh" content="0;url=http://evil.example/" />
METTA 地点
<meta http-equiv="location" content="URL=http://evil.example" />
BASE 劫持
<base href="http://evil.example/" />
有许多方法可以将您未预料到的客户端转到他们可能不希望访问的页面上(其中没有一个是依赖jQuery):
https://code.google.com/p/html5security/wiki/RedirectionMethods
我还要指出,人们不喜欢随机调整方向。 只有在绝对需要的时候才会调整方向。 如果您开始随机调整方向, 他们就不会再去你的网站了 。
下一段是假设性的:
您也可以被报告为恶意网站。如果发生这种情况,当人们点击与您网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。同样可能发生的是搜索引擎可能会开始降低您的评级,如果有人在您的网站上报告有不良经历的话。
请审查Google网站技术主管指南关于改方向的内容:https://support.google.com/webmasters/answer/2721217?hl=en&ref_topic=6001971
这是一个有趣的小页 踢你出页面。
<!DOCTYPE html>
<html>
<head>
<title>Go Away</title>
</head>
<body>
<h1>Go Away</h1>
<script>
setTimeout(function(){
window.history.back();
}, 3000);
</script>
</body>
</html>
如果您将两个页面示例合并在一起, 你会有一个新生的路线改变循环, 这将保证您的用户将永远不再想要使用您的网站 。
在单击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.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>
我们不能简单地使用 jQuery 重置
j/ 查询没有必要,window.location.replace(...)
最好模拟 HTTP 重定向 。
window.location.replace(...)
优于使用window.location.href
,因为replace()
用户不会陷入永无止境的后端故障中。
如果您想要模拟某人点击链接, 请使用location.href
如果您想要模拟 HTTP 重定向,请使用location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
<script type="text/javascript">
if(window.location.href === "http://stackoverflow.com") {
window.location.replace("https://www.google.co.in/");
}
</script>
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象