如果您想 simulate someone clicking on a link ,请使用 location.href .
如果您想 simulate an HTTP redirect ,请使用 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";
// 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";
6 回答
如果您想 simulate someone clicking on a link ,请使用
location.href
.如果您想 simulate an HTTP redirect ,请使用
location.replace
.例如:
从此答案复制到重复问题的信息
要重定向到另一个页面,您可以使用:
与
window.location="url";
相比,它更容易做location="url";
我总是使用它您可能需要再解释一下您的问题 .
当您说“重定向”时,对于大多数建议更改HTML页面位置的人:
当你说“重定向到功能”时 - 它真的没有意义 . 您可以调用函数,也可以重定向到其他页面 . 您甚至可以重定向并在加载新页面时调用一个函数 .
它比使用
window.location.href = 'http://sidanmor.com';
better使用
replace()
更好,因为它不会将原始页面保留在会话历史记录中,这意味着用户不会陷入永无止境的后退按钮惨败 .For example:
Taken from here: How to redirect to another page in jQuery?
您无法重定向到某个功能 . 您可以做的是在重定向时在URL上传递一些标志,然后在服务器端代码中检查该标志,如果引发该标志,则执行该功能 .
例如:
然后在您的PHP代码中检查查询字符串中的“action”,如果等于“DoThis”,则执行您需要的任何函数 .