The difference between Top.location and window.location.href

top.location.href= ”URL “open URL on top page (jump out of frame)

self.location.href = “URL” only open the URL address on this page

parent.location.href= ”URL opens the URL address in the parent window

this.location.href= ”The usage of “URL” is consistent with that of self

if ( top.location == self.location )Judge whether the current location is the top level to prohibit frame reference. If there is a custom frame in the page, you can also change the parent self top to the name of the custom frame. The effect is to open the URL in the custom frame window.

In practice, it may be used as follows:

1 if(top !== self){            
2 
3    top.location.href = location.href;
4 
5 }   //Disable frame references

The following is an example found on the Internet, which is not very intuitive. I added the three lines of code above, which can be removed first and then added. If you look at the effect, it will be very clear.

top.htm Code:

 1 <script language=javascript>
 2 
 3    function rs(){
 4 
 5     if(top !== self){
 6 
 7       top.location.href = location.href;
 8 
 9       }
10 
11       parent.left.location.href="top.htm" ;
12 
13     parent.bot.location.href="top.htm";
14 
15       }
16 
17 < /script>
18 
19 < input type=button name=name value="ksdj" onclick=rs();>

Here is an HTM file with a random file name:

 1 <FRAMESET COLS="150,*">
 2 
 3     < FRAME SRC="left.htm" name=left>
 4 
 5     < FRAMESET ROWS="150,*">
 6 
 7          < FRAME SRC="top.htm" name=top>
 8 
 9          < FRAME SRC="bot.htm" name=bot>
10 
11     < /FRAMESET>
12 
13     < /FRAMESET>  

Try, it may be this effect

Top represents the main window and location represents the current window. If your file has only one frame and no iframe or frmaeset, it is completely consistent and no difference.

top.location Is to open a new page in the top-level frame window.location Is to open a new page in the current frame

parent.location Open URL address in parent window of current window

Similar Posts: