使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
jj 查询不需要。 您可以做到这一点 :
window.open("URL","_self","","")
就这么简单!
提出HTTP要求的最佳方法是:document.loacation.href.replace('URL')
.
其他回答
以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :
<!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 中像这样重定向 :
$(location).attr('href', 'http://yourPage.com/');
这样做有三个主要途径,
window.location.href='blaah.com';
window.location.assign('blaah.com');
还有...
window.location.replace('blaah.com');
最后一个是最好的, 用于传统的重定向, 因为它不会保存您在搜索历史中被重定向之前的页面。 但是, 如果您只想用 JavaScript 打开一个标签, 您可以使用上述任何选项 。1
爱迪特:window
前缀是可选的 。
我们不能简单地使用 jQuery 重置
j/ 查询没有必要,window.location.replace(...)
最好模拟 HTTP 重定向 。
window.location.replace(...)
优于使用window.location.href
,因为replace()
用户不会陷入永无止境的后端故障中。
如果您想要模拟某人点击链接, 请使用location.href
如果您想要模拟 HTTP 重定向,请使用location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.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')
推荐文章
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 如何写setTimeout与参数Coffeescript