首页 > 编程知识 正文

分号的几种用法及例子,页面跳转的三种方式

时间:2023-05-05 15:02:31 阅读:264169 作者:2527

一、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。 
如<iframe name="aa"></iframe>   
  <input type=button   οnclick="window.open('1.htm','aa','')">和   
  <input type=button   οnclick="self.frames['aa'].location.href='1.htm'">的效果一样 

 

二、location.href不同形式之间的区别

a.html:

<form id="form1" action="">
<div><b>这是a.html页面<b>
<iframe src="b.html" width="500px" height="300px"></iframe> </b></b></div>
</form>
<pre>

b.html:

<span>这是b.html</span><br />
<iframe src="c.html" width="500px" height="300px"></iframe>

c.html:

<span><b>这是c.html:</b></span><br />
<iframe src="d.html" width="500px" height="300px"></iframe>

d.html:

<span>这是d.html:</span><br />
<input type='button' οnclick='jump();' value='跳转'>
<iframe src="d.html" width="500px" height="300px"></iframe>

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.rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="http://www.baidu.com";
//location.rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="http://www.baidu.com";
//self.location.rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="http://www.baidu.com";
//this.location.rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="http://www.baidu.com";
//location.rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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()是只能在一个网站中打开本网站的网页 

本文参考互联网上多篇博客编辑修改完成,展示出来供大家参考使用

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。