window location href页面跳转的几种用法及其延展 | 您所在的位置:网站首页 › 新打开的窗口总是在后面 › window location href页面跳转的几种用法及其延展 |
一、location.href常见的几种形式
self.location.href;//当前页面打开URL页面window.location.href;//当前页面打开URL页面this.location.href;//当前页面打开URL页面location.href;// 当前页面打开URL页面parent.location.href;//在父页面打开新页面top.location.href;//在顶层页面打开新页面
如果页面中自定义了frame,那么可将parent、self、top换为自定义frame的名称,效果是在frame窗口打开url地址。 此外,window.location.href=window.location.href;和window.location.Reload();都是刷新当前页面。 区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,window.location.href=window.location.href;则是向指定的url提交数据. 用window.open()打开新页面 但是用window.location.href="" 却是在原窗口打开的. 有时浏览器会一些安全设置window.open肯定被屏蔽。例如避免弹出广告窗口。 window.location是window对象的属性,而window.open是window对象的方法 window.location是你对当前浏览器窗口的URL地址对象的参考! window.open是用来打开一个新窗口的函数! 然而 window,open()不一定是打开新窗口 在有窗口的名称和window.open中第二个参数中的一样就会将这个窗口替换,用这个特性的话可以在iframe和frame中来代替location.href。 如 和 的效果一样 二、location.href不同形式之间的区别 a.html: 这是a.html页面 b.html: 这是b.html c.html: 这是c.html: d.html: 这是d.html: a.html里面嵌着b.html;b.html里面嵌着c.html;c.html里面嵌着d.html 在d.html里面添加js: function jump() { //经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转 //作用一样 window.location.href="http://www.baidu.com"; //location.href="http://www.baidu.com"; //self.location.href="http://www.baidu.com"; //this.location.href="http://www.baidu.com"; //location.href="http://www.baidu.com"; } 刷新a.html,点击"跳转" ,则d.html跳转到百度页面 修改d.html里面的js部分为: function jump() { parent.location.href='http://www.baidu.com'; } 则c.html 跳转为百度,这就说明了"parent.location.href是上一层页面跳转"即父级页面跳转。 再次修改d.html里面的js部分为: function jump() { top.location.href='http://www.baidu.com'; } 刷新a.html后,再次点击"跳转" 按钮,a.html已经跳转到了百度首页,这就解释了"top.location.href是最外层的页面跳转"即为最上层页面跳转。 三、location.href总结"top.location.href"是最外层的页面跳转(最上层) "window.location.href"、"location.href"是本页面跳转 "parent.location.href"是上一层页面跳转(父级). location是window对象的属性,而所有的网页下的对象都是属于window作用域链中(这是顶级作用域),所以使用时是可以省略window。而top是指向顶级窗口对象,parent是指向父级窗口对象。 延展window.location或window.open如何指定target? //方法一 window.location 改为 top.location 即可在顶部链接到指定页 //方法二 window.open("你的网址","_top"); 另: window.open()是可以在一个网站上打开另外的一个网站的地址 window.location()是只能在一个网站中打开本网站的网页 本文参考互联网上多篇博客编辑修改完成,展示出来供大家参考使用
|
CopyRight 2018-2019 实验室设备网 版权所有 |