使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
jj 用于重定向页面或 URL 的查询代码
第一路
这是用于重定向页面的 jQuery 代码 。 既然, 我已经把这个代码放在$( document). ready () 函数上, 一旦页面加载, 它将会立即执行 。
var url = "http://stackoverflow.com";
$(location).attr('href',url);
您甚至可以直接将一个 URL 直接传送到attr()方法,而不是使用变量。
第二路
window.location.href="http://stackoverflow.com";
您也可以使用这样的代码(两者在内部相同):
window.location="http://stackoverflow.com";
如果您对窗口. 位置和window.location.href
,然后你可以看到后一个设置href
明确表明财产,而前者默示地这样做。window.location
返回一个对象,该对象默认设置它.href
财产。
第三路
使用 JavaScript (JavaScript)replace()
方法方法的计算方法的计算方法window.location
对象。您可以将新的 URL 传送到replace()
方法, 它将模拟 HTTP 重定向。 顺便说一下, 记住window.location.replace()
方法不会在会话历史中放置原始页面, 这可能影响到后按钮的行为。 有时, 它会是你想要的, 所以要小心使用它 。
// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");
第四路
缩略location.assign()
方法在浏览器窗口中装入新文档。
window.location.assign("http://stackoverflow.com");
和(或)assign()
和replace()
方法为location.replace()
从文档历史中删除当前 URL 的方法,因此无法返回原始文档。您不能使用浏览器。后退按钮。如果您想要避免这种情况,应该使用location.assign()
方法,因为它在浏览器中装入一个新的文档。
第五路
类似attr()方法 (在 jQuery 1. 6 引入后)
var url = "http://stackoverflow.com";
$(location).prop('href', url);
其他回答
我也认为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.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.assign("http://google.com")
位置.href
定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...
用法:值应该被指定到它中 。
例如:
location.href = "http://google.com"
位置. replace () :
如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。
用法:数值应传递到它中。
例如:
location.replace("http://google.com")
assign()
和href
两者相似,可以保留历史。assign
通过分配,通过传递一个价值和粗略的作品来工作。
您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :
window.location = "http://google.com"
location.href = "http://google.com"
使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,
$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");
你可以很容易地理解 两者之间的区别...
这里是定位对象,
# HTML 页面使用 jQuery/JavaScript 方法重定向
尝试此示例代码 :
function YourJavaScriptFunction()
{
var i = $('#login').val();
if (i == 'login')
window.location = "Login.php";
else
window.location = "Logout.php";
}
如果您想要给完整 URL 提供完整 URLwindow.location = "www.google.co.in";
.
简在JavaScript 贾斯克里普特,您可以通过下列方式将方向转向特定页面:
window.location.replace("http://www.test.com");
或
location.replace("http://www.test.com");
或
window.location.href = "http://www.test.com";
使用 j 查询 :
$(window).attr("location","http://www.test.com");