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!

Why is location.href not executed immediately?

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
0
0
US
I have a
Code:
top.location.href
statement followed by an
Code:
alert
. The
Code:
alert
should never be executed because the
Code:
top.location.href
statement should update the all the frames, so the alert should not be seen. Right? After I click OK on the alert, the frames ARE all updated. (I only tried this on IE 6).

Code:
html>
<head>
  <title>top.location.href test</title>
</head>
<body>
<script type=&quot;text/javascript&quot;>
top.location.href=&quot;[URL unfurl="true"]http://www.collectiblesbycarole.com/&quot;;[/URL]
// Why isn't the location.href statement above executed immediately?
// The alert below should not be executed, but is.
alert(&quot;Why is this executed?&quot;);
</script>
</body>
</html>
 
I'm stumped!! It shouldn't do that. Normally after it changes url the javascript engine should be turned off.

Gary Haran
==========================
 
Additional info:

Same behavior on Netscape 7.02.

I too, am embaffleated.

G.
 
Well, I still don't know why the behavior occurs, but I do know how to workaround the problem: terminate the JavaScript segment and begin a new one. The new example below works as desired.

Code:
<html>
<head>
  <title>top.location.href test</title>
</head>
<body>

<script type=&quot;text/javascript&quot;>
top.location.href=&quot;[URL unfurl="true"]http://www.collectiblesbycarole.com/&quot;;[/URL]
</script>

<script type=&quot;text/javascript&quot;>
alert(&quot;No longer executed?&quot;);
</script>

</body>
</html>
 
banner.png


The reason why it works in the second example is due to the fact that modern browsers use a Document Object Model (DOM) type internal structure. They can only process a certain html tag when they have received all the data for it -- i.e when it has received all the markup up to and including the close tag. If an entire page is nested within an element, say a
Code:
<div>
for arguements sake, then the whole page will only display when the entire markup has been received.

So, in the second example the top.location.href = www.keteracel.com will be processed on it's own.

As for the first example, the javascript processor does not stop when the top.location.href line is reached. It sends the request to the browser (and the browser to the operating system) and then continues. I guess that it is all just a matter of timing -- if you look at the status bar in internet explorer you can see that the page has started loading but has been suspended by the alert, suggesting that the browser has simply not had enough time to complete the request before the alert has popped up.

Hope you find this useful,

keteracel



[sub]( keywords: DOM document object model top.location.href location.href alert javascript keteracel browser )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top