首页 > 代码库 > top.location.href和localtion.href有什么不同

top.location.href和localtion.href有什么不同

top.location.href=http://www.mamicode.com/”url” 在顶层页面打开url(跳出框架)

self.location.href=http://www.mamicode.com/”url” 仅在本页面打开url地址   

parent.location.href=http://www.mamicode.com/”url” 在父窗口打开Url地址   

this.location.href=http://www.mamicode.com/”url”  用法和self的用法一致

if (top.location == self.location) 判断当前location 是否为顶层来 禁止frame引用

如果页面当中有自定义的frame的话,也可以将parent  self   top换为自定义frame的名称效果就是在自定义frame窗口打开url地址



实际中可能这样使用


if(top !== self){ 

    top.location.href = http://www.mamicode.com/location.href;

}   禁止frame引用


 


以下是从网上找到的一个例子,不是很直观, 我加了上面那三行代码, 可以先去掉, 再加上, 看一下效果,就很清楚了 以下是top.htm 代码

<script language=javascript> function rs(){

if(top !== self){

    top.location.href = http://www.mamicode.com/location.href;

}

parent.left.location.href="http://www.mamicode.com/top.htm"

parent.bot.location.href="http://www.mamicode.com/top.htm" 

< /script> 

 input type=button name=name value="http://www.mamicode.com/ksdj" onclick=rs();>

以下是一个随意文件名的htm文件: <FRAMESET COLS="150,*"> < FRAME SRC="http://www.mamicode.com/left.htm" name=left> < FRAMESET ROWS="150,*"> < FRAME SRC="http://www.mamicode.com/top.htm" name=top> < FRAME SRC="http://www.mamicode.com/bot.htm" name=bot> < /FRAMESET> < /FRAMESET> 你自己试试,我想你要的可能就是这样的效果!


top表示主窗口,location表示当前窗口,如果你的文件只有一个框架,没有iframe和frmaeset,那么是完全一致的,没有区别。


top.location是在顶层frame中打开新页   window.location是在当前frame中打开新页

parent.location 在当前窗口的父窗口打开Url地址

 

 

top表示主窗口,location表示当前窗口,如果你的文件只有一个框架,没有iframe和frmaeset,那么是完全一致的,没有区别。

 


top.location.href和localtion.href有什么不同