使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
标注描述 :
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
杰克里:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
其他回答
我也认为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 -->
标注描述 :
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
杰克里:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
这很容易执行。 您可以使用 :
window.location.href = "http://www.example.com/";
这将记住上一页的历史。 这样可以点击浏览器的后键返回历史 。
或者:
window.location.replace("http://www.example.com/");
此方法不记得上一页的历史。 在此情况下, 后端按钮会被禁用 。
所以,问题在于如何改变页面方向, 而不是如何改变网页方向?
您只需要为此使用 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;
使用 JavaScript :
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 j 查询 :
方法1:美元(地点)
$(location).attr('href', 'http://google.com');
方法2:可再使用功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");