页面跳转方式及区别

306 阅读1分钟

背景

在项目中有的跳转是需要新开页,有些需要在当前页面跳转

页面跳转的方式有哪些?

window.location(.href) = ""

window.location = "http://"'
window.location.href = "https//"

上面两种方法都是当前页面跳转; 不同之处在于 window.location 返回的是对象,如果没有.href,它的默认参数就是href

window.open

window.open("https://")
window.open("https://", "_self");   

上面两种方法不带参数_self,会走新开页面,加self在当前页面跳转 window.open("http://", "_self")与window.location相比,window.location有IE浏览器兼容问题,会有缓存存在; 所以项目中采用了,window.open("https://", "_self");

其他跳转方式扩展

  1. window.location.reload(); // 重新加载当前页面
  2. window.location.replace(); // 重新加载且可以替换的当前页面为另一个页面;
  3. top.location.href=”url” // 在顶层页面打开url(跳出框架)
  4. self.location.href=”url” //仅在本页面打开url地址
  5. parent.location.href=”url” // 在父窗口打开Url地址
  6. this.location.href=”url” // 用法和self的用法一致