使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
我已经使用 JavaScript 的函数重定向() 。 它正在工作 。
<script type="text/javascript">
$(function () {
//It's similar to HTTP redirect
window.location.replace("http://www.Technomark.in");
//It's similar to clicking on a link
window.location.href = "Http://www.Technomark.in";
})
</script>
其他回答
警告: 警告:答案只是作为一种可能的解决办法而已;显然否最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。
$(location).prop('href', 'http://stackoverflow.com')
每个浏览器都使用此功能 :
window.location.href = 'your_url';
您可以使用以下方法重定向页面:
通过在头部使用元标记 -
<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />
注意:content="0;
... 用于在您需要多少秒后重定向页面使用 JavaScript :
window.location.href = "http://your-page-url.com";
使用 jQuery 查询 :
$(location).attr('href', 'http://yourPage.com/');
使用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);
}
从客户的侧面调整方向:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript and jQuery example to redirect a page or URL </title>
</head>
<body>
<div id="redirect">
<h2>Redirecting to another page</h2>
</div>
<script src="scripts/jquery-1.6.2.min.js"></script>
<script>
// JavaScript code to redirect a URL
window.location.replace("http://stackoverflow.com");
// window.location.replace('http://code.shouttoday.com');
// Another way to redirect page using JavaScript
// window.location.assign('http://code.shouttoday.com');
// window.location.href = 'http://code.shouttoday.com';
// document.location.href = '/relativePath';
//jQuery code to redirect a page or URL
$(document).ready(function(){
//var url = "http://code.shouttoday.com";
//$(location).attr('href',url);
// $(window).attr('location',url)
//$(location).prop('href', url)
});
</script>
</body>
</html>
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象