js 页面跳转和刷新的方式以及不同

1,418 阅读2分钟

window.location.href = url

     在当前页面(原窗口)打开url

window.location.assign(url); 

    触发窗口加载并显示指定的URL的内容,相当于window.location.href。详情

window.locaton.replace(url);

     以给定的URL来替换当前的资源。与assign() 方法 不同的是,调用 replace() 方法后,当前页面不会保存到会话历史中(session History),这样,用户点击回退按钮时,将不会再跳转到该页面。详情

window.open(strUrl, strWindowName, [strWindowFeatures]);

strUrl 要在新打开的窗口中加载的URL。

strWindowName 新窗口的名称。

strWindowFeatures 一个可选参数,列出新窗口的特征(大小,位置,滚动条等)作为一个

     用指定的名称将指定的资源加载到浏览器上下文(窗口 window ,内嵌框架 iframe 或者标签 tab )。如果没有指定名称,则一个新的窗口会被打开并且指定的资源会被加载进这个窗口的浏览器上下文中。(可能被浏览器拦截)

创建 a 标签

let a = document.createElement("a");           
a.setAttribute("href", url);      
a.setAttribute("target", "_blank");
a.click();
     打开页面不被浏览器拦截。

历史页面跳转

window.history.back();

    等同于点击浏览器的回退按钮。

window.history.go(delta);

     delta相对于当前页面你要去往历史页面的位置。负值表示向后移动,正值表示向前移动。因此,例如:history.go(2)向前移动两页,history.go(-2)则向后移动两页。如果未向该函数传参或delta相等于0,则该函数与调用location.reload()具有相同的效果。

location.href 的几种方式

self.location.href;//当前页面打开URL页面

window.location.href;//当前页面打开URL页面

this.location.href;//当前页面打开URL页面

location.href;// 当前页面打开URL页面

parent.location.href;//在父页面打开新页面

top.location.href;//在顶层页面打开新页面

     location是window对象的属性,而所有的网页下的对象都是属于window作用域链中(这是顶级作用域),所以使用时是可以省略window。