如何用JavaScript从另一个页面重定向到一个页面?
当前回答
如果您想模拟某人点击链接,请使用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";
从这个答案复制到一个重复问题的信息
其他回答
如果您想模拟某人点击链接,请使用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 = "http://www.yoururl.com";
你不能重定向到一个函数。你可以做的是在重定向时在URL上传递一些标志,然后在服务器端代码中检查该标志,如果被引发,则执行该函数。
例如:
document.location = "MyPage.php?action=DoThis";
然后在PHP代码中检查查询字符串中的“action”,如果等于“DoThis”,则执行所需的任何函数。
相比window.location="url";使用location="url"就简单多了;我总是用这个
window.location.replace('http://sidanmor.com');
这比使用window.location.href = 'http://sidanmor.com'要好;
使用replace()更好,因为它不会在会话历史中保留原始页面,这意味着用户不会陷入无休止的后退按钮的混乱中。
如果您想模拟某人点击链接,请使用 window.location.href 如果您想模拟HTTP重定向,请使用window.location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://sidanmor.com");
// similar behavior as clicking on a link
window.location.href = "http://sidanmor.com";
从这里开始: 如何在jQuery重定向到另一个页面?
推荐文章
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?