使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
所以,问题在于如何改变页面方向, 而不是如何改变网页方向?
您只需要为此使用 JavaScript 即可。 这里有一些小代码, 可以创建一个动态重定向页面 。
<script>
var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
if( url ) window.location.replace(url);
</script>
所以说,你只是把这个片段redirect/index.html
在您网站上的文档中,您可以这样使用它 。
http://www.mywebsite.com/redirect?url=http://stackoverflow.com
如果你去那个链接 它会自动引导你到堆叠流. com.
这就是你如何制造简单使用 JavaScript 重定向页面
编辑 :
还有一件事需要指出。window.location.replace
在我的代码中,因为我认为它适合 重置页面, 但是,你必须知道,当使用window.location.replace
当您在浏览器中按回按钮时,它会被重定向否回到重定向页面, 它会回到之前的页面, 看看这个小演示的东西。
示例:
进程:家居存储店 => 将页面重定向到谷歌 => 谷格
当谷歌:谷格 => 在浏览器中的后退按钮 => 家居存储店
因此,如果这适合您的需求, 那么一切都应该很好。 如果您想要在浏览器历史中包含重置页面, 替换此
if( url ) window.location.replace(url);
与
if( url ) window.location.href = 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>
这样做有三个主要途径,
window.location.href='blaah.com';
window.location.assign('blaah.com');
还有...
window.location.replace('blaah.com');
最后一个是最好的, 用于传统的重定向, 因为它不会保存您在搜索历史中被重定向之前的页面。 但是, 如果您只想用 JavaScript 打开一个标签, 您可以使用上述任何选项 。1
爱迪特:window
前缀是可选的 。
JavaScript 非常广泛。 如果您想要跳转到另一页, 您有三个选项 。
window.location.href='otherpage.com';
window.location.assign('otherpage.com');
//and...
window.location.replace('otherpage.com');
当您想要移动到另一页时, 如果这是您的要求, 您可以使用其中的任何内容 。 但是所有三种选择都局限于不同的情况 。 请按照您的要求明智地选择 。
如果您有兴趣了解更多有关这个概念的知识,可以更深入地了解。
window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.
每个浏览器都使用此功能 :
window.location.href = 'your_url';
警告: 警告:答案只是作为一种可能的解决办法而已;显然否最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。
$(location).prop('href', 'http://stackoverflow.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切换类