使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
<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>
其他回答
在单击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});
警告: 警告:答案只是作为一种可能的解决办法而已;显然否最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。
$(location).prop('href', 'http://stackoverflow.com')
<script type="text/javascript">
if(window.location.href === "http://stackoverflow.com") {
window.location.replace("https://www.google.co.in/");
}
</script>
所以,问题在于如何改变页面方向, 而不是如何改变网页方向?
您只需要为此使用 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;
这是重定向到其他页面的代码 超过10秒的超时
<script>
function Redirect()
{
window.location="http://www.adarshkr.com";
}
document.write("You will be redirected to a new page in 10 seconds.");
setTimeout('Redirect()', 10000);
</script>
您也可以这样操作, 点击按钮时使用位置. 指派 :
<input type="button" value="Load new document" onclick="newPage()">
<script>
function newPage() {
window.location.assign("http://www.adarshkr.com")
}
</script>