Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.location VS top.location.replace

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
0
0
CA
I see someone doing the following

Code:
<SCRIPT LANGUAGE="JavaScript">
    function refresh ()
    {
        top.location.replace("[URL unfurl="true"]http://www.mydomain.com/pages/home/main.shtml");[/URL]
    }
</SCRIPT>
</HEAD>
<BODY onLoad="refresh()">
</BODY>

I wonder why they use
top.location.replace

they could have use
window.location="
is there any difference between these two? They should produce the same results right?
 
Wrong.

The code above is commonly used to break out of a frameset.

Within a frame, 'window' is the current frame, but 'top' is always the top-most window.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks for your reply.


The body of the page calling this function is

Code:
<BODY onLoad="refresh()">
</BODY>

Its the index.shtml file. And as you see there is not frameset there. so not sure why someone will use
top.location.replace. Look like there are simply redirecting to different main.shtml
 
Some people do this to break out of frames that are there unintentionally (for instance: `google images` searches, where google makes a frameset to show your search info on top and the original page below).

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
And as you see there is not frameset there.

Not now there isn't... but as Kirsle says, there might be without the site author intending it. This is why is is called 'frame breaker' code.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

To me than is not a typical frame breaker.
[ul]
[li]The frame breaker should be conditional.[/li]
[li]While should be present in all documents, is easier to not use a hardcoded URL.[/li]
[li]Usually they not wait until the whole document is loaded.[/li]
[/ul]
Code:
if (top!=window) top.location=window.location
The code posted by spicymango indeed breaks out from frames, but I would say that is just a side effect. Its primary purpose seems to be redirecting.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top