我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
如何从客户端侧进行重定向:
<!DOCTYPE html> <html> <head> <title>JavaScript and jQuery example to redirect a page or URL </title> </head> <body> <div id="redirect"> <h2>Redirect to another page</h2> </div> <script src="scripts/jquery-1.6.2.min.js"></script> <script> // JavaScript code to redirect a URL window.location.r
其他回答
警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。
$(location).prop('href', 'http://stackoverflow.com')
在我的工作经验中,JavaScript更好地重定向。
它取决于您想要如何更改位置. 如果您想在用户历史中登录您的网站,请使用 window.location.href='ur website';. 否则,如果您不登录历史,请使用 window.location.replace(“您的网站”);。
在JavaScript和jQuery中,我们可以使用以下代码将一页转向另一页:
window.location.href="http://google.com";
window.location.replace("page1.html");
在 PHP、HTML 或 jQuery 部分后写下下面的代码,如果在 PHP 或 HTML 部分中间,则使用 <script> 标签。
location.href = "http://google.com"
jQuery 代码重新引导页面或 URL
您甚至可以直接将 URL 转移到 attr() 方法,而不是使用变量。
第二条路
window.location.href="http://stackoverflow.com";
你也可以编码如下(两者都是内部相同的):
window.location="http://stackoverflow.com";
第三种方式
第四条路
Location.assign() 方法将新文档上传到浏览器窗口中。
window.location.assign("http://stackoverflow.com");
第五条路
如 attr() 方法( jQuery 1.6 引入后)
var url = “http://stackoverflow.com”; $(位置)。prop('href', url);
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?