我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
我只是用另一个更新的jQuery方法更新这个荒谬:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);
其他回答
因此,问题是如何创建一个重定向页面,而不是如何重定向到一个网站?
<script>
var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
if( url ) window.location.replace(url);
</script>
http://www.mywebsite.com/redirect?url=http://stackoverflow.com
如果你去那个链接,它会自动将你转向stackoverflow.com。
这就是你用JavaScript创建一个简单的重定向页面的方式。
我添加了 window.location.replace 在我的代码,因为我认为它适合一个重定向页面,但是,你必须知道,当使用 window.location.replace 和你得到重定向,当你按下后按钮在你的浏览器它不会回到重定向页面,它会回到页面之前,看看这个小演示事物。
例子:
因此,如果这符合您的需求,那么一切都应该顺利。 如果您想将重定向页面在浏览器历史中取代这个
if( url ) window.location.replace(url);
与
if( url ) window.location.href = url;
首先写得正确. 你想在一个应用程序内导航另一个链接从你的应用程序另一个链接. 这里是代码:
window.location.href = "http://www.google.com";
如果你想在你的应用程序内浏览页面,那么我也有代码,如果你愿意。
使用 JavaScript:
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 jQuery:
方法1: $(位置)
$(location).attr('href', 'http://google.com');
方法2:可重新使用的功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");
单页申请,在同一申请路线内
window.location.pathname = '/stack';
JavaScript 的内容:
location.href = "http://stack.com";
window.location = "http://stack.com";
jQuery:
$(location).attr('href', "http://www.stack.com");
$(window).attr('location', "http://www.stack.com");
角质4
import { Router } from '@angular/router';
export class NavtabComponent{
constructor(private router: Router) {
}
this.router.navigate(['bookings/taxi']);
}
我也认为 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切换类