我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
JavaScript 为您提供了许多方法,以获取和更改浏览器的地址栏中显示的当前 URL. 所有这些方法都使用位置对象,这是窗口对象的属性. 您可以创建一个新的位置对象,具有当前 URL。
var currentLocation = window.location;
一个URL的基本结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
此分類上一篇
协议 - 指定协议名称用于访问互联网上的资源. (HTTP(没有SSL)或HTTPS(有SSL))主机名称 - 主机名称指定主机拥有资源. 例如, www.stackoverflow.com. 服务器提供服务,使用主机名称。
使用这些位置对象属性,您可以访问所有这些 URL 组件
现在,如果您想要更改一个页面或将用户转向另一个页面,您可以使用位置对象的href属性,如此。
您可以使用位置对象的 href 属性。
window.location.href = "http://www.stackoverflow.com";
位置对象也有这些三种方法
location.assign("http://www.stackoverflow.com");
location.replace("http://www.stackoverflow.com");
$(location).attr('href',url);
其他回答
如果您想在同一应用程序中重新引导到路线
window.location.pathname = '/examplepath'
这将是走路的方式。
试试这:
location.assign("http://www.google.com");
编码的例子。
此分類上一篇: 你可以將延遲時間設定到你想要的任何地方:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Document Title</title>
<script type="text/javascript">
function delayer(delay) {
onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
}
</script>
</head>
<body>
<script>
delayer(8000)
</script>
<div>You will be redirected in 8 seconds!</div>
</body>
</html>
使用 jQuery/JavaScript 方法重定向 HTML 页面
试试这个例子代码:
function YourJavaScriptFunction()
{
var i = $('#login').val();
if (i == 'login')
window.location = "Login.php";
else
window.location = "Logout.php";
}
如果您想要提供一个完整的URL,如 window.location = “www.google.co.in”;
我也认为 location.replace(URL)是最好的方式,但如果你想通知搜索引擎你的重新引导(他们不分析JavaScript代码查看重新引导),你应该添加 rel="canonical" meta 标签到你的网站。
添加一个Noscript 部分,其中有一个 HTML 更新 meta 标签,也是一个很好的解决方案. 我建议您使用这个 JavaScript 重定向工具来创建重定向。
样品代码无延迟看起来如下:
<!-- 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 -->
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 登录后,Laravel重定向回原始目的地
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类