我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
您可以使用 jQuery 如下:
window.location = "http://yourdomain.com";
如果你只想要jQuery,那么你可以这样做:
$jq(window).attr("location","http://yourdomain.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']);
}
标准“瓦尼拉”JavaScript如何重新引导页面
window.location.href = 'newPage.html';
location.href = 'newPage.html';
以下部分适用于使用 HTTP_REFERER 作为许多安全措施之一(尽管这不是一个很好的保护措施)。如果您使用 Internet Explorer 8 或更低,这些变量在使用任何形式的 JavaScript 页面重定向(location.href 等)时会失去。
下面我们将实施一个替代 IE8 和更低,以免失去 HTTP_REFERER. 否则,你几乎总是可以简单地使用 window.location.href。
对 HTTP_REFERER 进行测试(URL 粘贴、会话等)可以帮助您知道请求是否合法(注:在评论中提到 drop 的链接中,还有一些方法可以工作周围 / 扫描这些引用器)
简单的跨浏览器测试解决方案(下载到 window.location.href for Internet Explorer 9+ 和所有其他浏览器)
使用:重定向(“anotherpage.aspx”);
function redirect (url) {
var ua = navigator.userAgent.toLowerCase(),
isIE = ua.indexOf('msie') !== -1,
version = parseInt(ua.substr(4, 2), 10);
// Internet Explorer 8 and lower
if (isIE && version < 9) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
}
// All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
else {
window.location.href = url;
}
}
<script type="text/javascript">
if(window.location.href === "http://stackoverflow.com") {
window.location.replace("https://www.google.co.in/");
}
</script>
使用 jQuery/JavaScript 重新引导用户
使用 jQuery 或 JavaScript 的位置对象,我们可以将用户转向另一个网页。
在 jQuery
将用户从一页转向另一页的代码是:
var url = 'http://www.example.com';
$(location).attr('href', url);
在JavaScript
将用户从一页转向另一页的代码是:
var url = 'http://www.example.com';
window.location.href = url;
或
var url = 'http://www.example.com';
window.location = url;
你可以使用:
window.location.replace("http://www.example.com/");
替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。
但是,如果你想要一个与点击链接相同的效果,你应该去:
window.location.href = "http://www.example.com/";
在此情况下,浏览器返回按钮将是活跃的。
推荐文章
- 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切换类