如何用JavaScript从另一个页面重定向到一个页面?
当前回答
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重定向到另一个页面?
其他回答
相比window.location="url";使用location="url"就简单多了;我总是用这个
要重定向到另一个页面,您可以使用:
window.location = "http://www.yoururl.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";
从这个答案复制到一个重复问题的信息
你可能需要再解释一下你的问题。
当你说“重定向”时,对于大多数建议更改HTML页面位置的人:
window.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 ?