使用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;
其他回答
标注描述 :
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
杰克里:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
这是重定向到其他页面的代码 超过10秒的超时
<script>
function Redirect()
{
window.location="http://www.adarshkr.com";
}
document.write("You will be redirected to a new page in 10 seconds.");
setTimeout('Redirect()', 10000);
</script>
您也可以这样操作, 点击按钮时使用位置. 指派 :
<input type="button" value="Load new document" onclick="newPage()">
<script>
function newPage() {
window.location.assign("http://www.adarshkr.com")
}
</script>
使用location.replace()
这将调整您的方向, 但不保存上一页的历史。 提交表格时最好使用此选项 。
但当你想保留自己的历史时,你必须使用location.href=//path
.
实例:
// Form with steps
document.getElementById('#next').onclick = function() {
window.location.href='/step2' // Iteration of steps;
}
// Go to next step
document.getElementById('#back').onclick = function() {
window.history.back();
}
// Finish
document.getElementById('#finish').onclick = function() {
window.location.href = '/success';
}
// On success page
window.onload = function() {
setTimeout(function() {
window.location.replace('/home'); // I can't go back to success page by pressing the back button
},3000);
}
使用 JavaScript :
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 j 查询 :
方法1:美元(地点)
$(location).attr('href', 'http://google.com');
方法2:可再使用功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.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