使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
首先正确写入。 您想要在应用程序中浏览另一个链接, 从应用程序中浏览另一个链接。 以下是代码 :
window.location.href = "http://www.google.com";
如果您想要浏览您应用程序中的页面, 我也有代码, 如果您想要的话 。
其他回答
指定位置( ) :
将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。
用法:数值应传递到它中 。
例如:
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");
你可以很容易地理解 两者之间的区别...
这里是定位对象,
首先正确写入。 您想要在应用程序中浏览另一个链接, 从应用程序中浏览另一个链接。 以下是代码 :
window.location.href = "http://www.google.com";
如果您想要浏览您应用程序中的页面, 我也有代码, 如果您想要的话 。
我也认为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 -->
如果您喜欢使用纯 JavaScript 我意识到使用document.location.href = "https://example.com"
或window.location.href = "https://example.com"
导致 Firefox 的兼容问题。 相反, 尝试使用 :
location.href = "https://example.com";
location.replace("http://example.com");
我的情况解决了问题 祝你好运!
这样做有许多方法。
// window.location
window.location.replace('http://www.example.com')
window.location.assign('http://www.example.com')
window.location.href = 'http://www.example.com'
document.location.href = '/path'
// window.history
window.history.back()
window.history.go(-1)
// window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')
// Probably no bueno
self.location = 'http://www.example.com';
top.location = 'http://www.example.com';
// jQuery
$(location).attr('href','http://www.example.com')
$(window).attr('location','http://www.example.com')
$(location).prop('href', 'http://www.example.com')