使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :
<!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>
其他回答
根据我的工作经验 JavaScript 更适合重新定位
它取决于您要如何更改地址。 如果您想要在用户历史中登录您的网站, 请使用window.location.href='ur website';
否则在不记录历史的情况下使用window.location.replace("your website");
.
警告: 警告:答案只是作为一种可能的解决办法而已;显然否最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。
$(location).prop('href', 'http://stackoverflow.com')
var url = 'asdf.html';
window.location.href = url;
内jj 查询,使用$(location).attr('href', url)
:
$(document).ready(function(){
var url = "https://www.youtube.com/watch?v=JwMKRevYa_M";
$(location).attr('href', url); // Using this
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
内原始 JavaScript 贾斯克里普特实现这一点有若干途径:
window.location.href="https://www.youtube.com/watch?v=JwMKRevYa_M";
- 明确设定 href 属性。
window.location = "http://www.GameOfThrones.com";
- 是否暗含它 因为窗口。 位置返回一个对象, 默认设置它的. href 属性 。
window.location.replace("http://www.stackoverflow.com");
- 将当前窗口的位置替换为新窗口。
self.location = "http://www.somewebsite.com";
- 设置当前窗口本身的位置。
以下是JavaScript在一定时间(3秒)后重新定向的例子:
<script>
setTimeout(function() {
window.location.href = "https://www.youtube.com/";
}, 3000);
</script>
以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :
<!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>
推荐文章
- 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切换类