如何用JavaScript从另一个页面重定向到一个页面?
当前回答
要重定向到另一个页面,您可以使用:
window.location = "http://www.yoururl.com";
其他回答
相比window.location="url";使用location="url"就简单多了;我总是用这个
要重定向到另一个页面,您可以使用:
window.location = "http://www.yoururl.com";
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";
从这个答案复制到一个重复问题的信息
你不能重定向到一个函数。你可以做的是在重定向时在URL上传递一些标志,然后在服务器端代码中检查该标志,如果被引发,则执行该函数。
例如:
document.location = "MyPage.php?action=DoThis";
然后在PHP代码中检查查询字符串中的“action”,如果等于“DoThis”,则执行所需的任何函数。
推荐文章
- 在数组中获取所有选中的复选框
- 如何为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方法