如何用JavaScript从另一个页面重定向到一个页面?
你可能需要再解释一下你的问题。
当你说“重定向”时,对于大多数建议更改HTML页面位置的人:
window.location = url;
当你说“重定向到函数”时,它真的没有意义。可以调用函数,也可以重定向到另一个页面。 您甚至可以重定向并在加载新页面时调用一个函数。
你不能重定向到一个函数。你可以做的是在重定向时在URL上传递一些标志,然后在服务器端代码中检查该标志,如果被引发,则执行该函数。
例如:
document.location = "MyPage.php?action=DoThis";
然后在PHP代码中检查查询字符串中的“action”,如果等于“DoThis”,则执行所需的任何函数。
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重定向到另一个页面?
如果您想模拟某人点击链接,请使用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";
从这个答案复制到一个重复问题的信息
推荐文章
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 登录后,Laravel重定向回原始目的地
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类