By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

When is window.location = "url" executed?

P: n/a
Hello,

the example code is the following(the number in parentheses at the
beginning are just for reference)(The complete HTML file is at the end
of this article):

(1)window.location = 'http://www.google.com';
(2)alert("I'm still here!");
(3)window.open("http://www.slashdot.com", "_blank");

What happens, is that after the assignment (1) theoretically the rest
of the code shouldn't execute, since the window should be loading a
new URL. But in my browser(Firefox 1.0 on GNU/Linux) the browser also
executes (2) and (3).
It seems that the window is only loaded with the new content after the
script has finished executing.
So is this behaviour wrong or correct?

In all documentations I searched there is no mention of WHEN the new
location should be loaded, if immediately or only after the script
finished executing.

Can anyone try this with IE?

Thank you

Roland

Complete file:

<html:html>
<head>
<script type="text/javascript">
window.location = 'http://www.google.com';
alert("I'm still here!");
window.open("http://www.slashdot.com", "_blank");
</script>
</head>
<body>
Test.
</body>
</html:html>
Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Roland wrote:
<snip>
(1)window.location = 'http://www.google.com';
(2)alert("I'm still here!");
(3)window.open("http://www.slashdot.com", "_blank");

What happens, is that after the assignment (1) theoretically
the rest of the code shouldn't execute, since the window
should be loading a new URL.
If your expectation is that the assignment to the location property is
the last operation then why is there code after it?

But no, the assignment to the location property does not replace the
page immediately, it might (probably does) make an HTTP request
immediately, that may ultimately return a response that will result in
the page being replaced. But that would be an asynchronous process
anyway so there is not reason to expect the script to terminate at the
point of the assignment.
But in my browser(Firefox 1.0 on GNU/Linux) the browser
also executes (2) and (3).
It seems that the window is only loaded with the new content
after the script has finished executing.
Most likely the page is only replaced when its replacement arrives from
the server.
So is this behaviour wrong or correct?
No specification defines behaviour for assignments to the location
object to the extent that any single behaviour could be expected.
In all documentations I searched there is no mention of
WHEN the new location should be loaded, if immediately
or only after the script finished executing.
It would be unrealistic to expect the page to be replaced prior to the
response to the HTTP request, and no reason to expect the HTTP
request/response process to confine, or be confined by, the execution of
scripts.
Can anyone try this with IE?

<snip>

To what end? If you don't want script to execute after the assignment to
the location property then don't put code after the assignment.

Richard.
Jul 23 '05 #2

P: n/a
You should do an IF with a BODY ONLOAD, but a simple EXIT, as shown
below, ought to answer the question.

<html>
<head>
<script type="text/javascript">
window.location = 'http://www.askblax.com';
exit(); // stage left
alert("Never gonna reach me!");
window.open("http://www.google.com", "_blank");

</script>
</head>
<body bgcolor=pink>
Without a valid test before, I will not redirect pages this
way.<BR><BR>Without a valid test before, I will not redirect pages this
way. The only way to reach this line is if a javascript error occurs
before the exit command.
</body>
</html>

http://www.askblax.com

Jul 23 '05 #3

P: n/a
>> Most likely the page is only replaced when its replacement arrives
from
the server. <<

I think you are right.
To what end? If you don't want script to execute after the

assignment to
the location property then don't put code after the assignment. <<

Frames maybe.

Jul 23 '05 #4

P: n/a
askMe wrote on 04 mei 2005 in comp.lang.javascript:
You should do an IF with a BODY ONLOAD, but a simple EXIT, as shown
below, ought to answer the question.

<html>
<head>
<script type="text/javascript">
window.location = 'http://www.askblax.com';
exit(); // stage left
exit()?

What language is that, I askYou?

alert("Never gonna reach me!");
window.open("http://www.google.com", "_blank");

</script>


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #5

P: n/a
Lee
askMe said:
Most likely the page is only replaced when its replacement arrivesfrom
the server. <<

I think you are right.
To what end? If you don't want script to execute after the

assignment to
the location property then don't put code after the assignment. <<

Frames maybe.


What about frames? That response doesn't seem to make any sense.
The assignment to the location property is going to clear the
rest of the code (sooner or later), anyway.

Jul 23 '05 #6

P: n/a

Lee wrote:
askMe said:
Most likely the page is only replaced when its replacement
arrivesfrom
the server. <<

I think you are right.
To what end? If you don't want script to execute after the

assignment to
the location property then don't put code after the assignment. <<

Frames maybe.


What about frames? That response doesn't seem to make any sense.
The assignment to the location property is going to clear the
rest of the code (sooner or later), anyway.


Your question was "to what end?"...

If I have a frame set of two pages and someone calls one of the pages
on its own, a test could be done from the page to see the current
parent/top window. If the result is not what is expected, redirect to
the parent/top to remake the frameset, else exit the javascript and
continue loading the current page because the page is already loading
as designed.

Expect the unexpected.

Jul 23 '05 #7

P: n/a
It is Javascript, I tellYou. :-)

http://www.askblax.com

Jul 23 '05 #8

P: n/a
askMe wrote:
If I have a frame set of two pages and someone calls one of the pages
on its own, a test could be done from the page to see the current
parent/top window. If the result is not what is expected, redirect to
the parent/top to remake the frameset,
Child's play. Within the frame document:

<head>
...
<script type="text/javascript">
/**
* @author
* (C) 2003, 2004 Thomas Lahn &lt;ty******@PointedEars.de&gt;
* Distributed under the GNU GPL v2 and above.
* @optional Object|string o
* Object to be determined an method, i.e. a
* <code>Function</code> object assigned as property of
* another object. May also be a string to be evaluated
* and so is applicable to unknown properties.
* @return type boolean
* <code>true</code> if <code>o</code> is a method,
* <code>false</code> otherwise.
* @see #isMethodType()
*/
function isMethod(m)
{
var t;
(m = eval(m)) && (t = typeof m);
return (t == "function" || t == "object");
}

if (typeof parent != "undefined"
&& typeof top != "undefined"
&& parent == top // not in a frameset
&& typeof location != "undefined")
{
var s = "my_frameset.html?" + encodeURIComponent(location);
if (isMethod("top.location.replace"))
{
top.location.replace(s);
}
else
{
top.location = s;
}
}
</script>
...
</head>

Within the frameset document:

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<frameset ... onload="if (location.search) frames[...].location =
decodeURIComponent(location.search.replace(/^\??(.*)/, '$1'));" ...>
...
</frameset>
else exit the javascript
A client-side script in a UA environment cannot be exited. It can simply
be not further executed, i.e. if no statement or expression follows (or
the current statement results in a fatal error), the script ends.
and continue loading the current page because the page is already loading
as designed.
See above. Redirection will not be performed if the document is part
of a frameset.
Expect the unexpected.


Learn how to use search engines.
PointedEars
Jul 23 '05 #9

P: n/a

Thomas 'PointedEars' Lahn wrote:
askMe wrote:
If I have a frame set of two pages and someone calls one of the pages on its own, a test could be done from the page to see the current
parent/top window. If the result is not what is expected, redirect to the parent/top to remake the frameset,
Child's play. Within the frame document:


Actually, manipulating frames is a pain in the. . . That is why I don't
use them if I don't have to.
<head>
...
<script type="text/javascript">
/**
* @author
* (C) 2003, 2004 Thomas Lahn &lt;ty******@PointedEars.de&gt; * Distributed under the GNU GPL v2 and above.
* @optional Object|string o
* Object to be determined an method, i.e. a
* <code>Function</code> object assigned as property of
* another object. May also be a string to be evaluated
* and so is applicable to unknown properties.
* @return type boolean
* <code>true</code> if <code>o</code> is a method,
* <code>false</code> otherwise.
* @see #isMethodType()
*/
function isMethod(m)
{
var t;
(m = eval(m)) && (t = typeof m);
return (t == "function" || t == "object");
}

if (typeof parent != "undefined"
&& typeof top != "undefined"
&& parent == top // not in a frameset
&& typeof location != "undefined")
{
var s = "my_frameset.html?" + encodeURIComponent(location);
if (isMethod("top.location.replace"))
{
top.location.replace(s);
}
else
{
top.location = s;
}
}
</script>
...
</head>

Within the frameset document:

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<frameset ... onload="if (location.search) frames[...].location =
decodeURIComponent(location.search.replace(/^\??(.*)/, '$1'));" ...>
...
</frameset>
else exit the javascript
A client-side script in a UA environment cannot be exited. It can

simply be not further executed,
Exiting is halting/aborting execution.
i.e. if no statement or expression follows (or
the current statement results in a fatal error), the script ends.

It will exit that way, too, although that is not the best way to end a
script.
and continue loading the current page because the page is already loading as designed.


See above. Redirection will not be performed if the document is part
of a frameset.


Hmmm... I don't think that is true. I struggled with a piece of code
recently that required redirection of frames. At one point, it seemed
to work, but I never got any consistent behavior so I ended up
scrapping it. Also, it is possible to refresh the top frame even if
not the subordinates. Of course all the frames can be loaded/reloaded
in response to events, so no major loss if redirection doesn't work.
Expect the unexpected.


Learn how to use search engines.


Search to learn engine use why.

http://www.askblax.com

PointedEars


Jul 23 '05 #10

P: n/a
askMe wrote:
Thomas 'PointedEars' Lahn wrote:
askMe wrote:
> If I have a frame set of two pages and someone calls one of the pages ^^^
*Please* either don't use Google Groups interface for posting or learn
how to post readable quotes.

[quoting repaired:]
> on its own, a test could be done from the page to see the current
> parent/top window. If the result is not what is expected, redirect
> to the parent/top to remake the frameset,


Child's play. Within the frame document:


Actually, manipulating frames is a pain in the. . .


Why?
That is why I don't use them if I don't have to.
Your logic is erroneous. What if client-side scripting is not even
supported?

I don't use frames if there are other viable solutions because otherwise
it would reduce usability. Think of no-script UAs, smaller viewports,
bookmarks etc.
[...]
It was completely unnecessary to quote all my code since you are not
directly referring to it. Please read the FAQ (Notes) and learn how
to post.
> else exit the javascript


A client-side script in a UA environment cannot be exited. It can
simply be not further executed,


Exiting is halting/aborting execution.


Yes. Just what I wrote: A client-side script in a UA cannot be
exited, it can only be not further executed. This is different.
i.e. if no statement or expression follows (or
the current statement results in a fatal error), the script ends.


It will exit that way, too, although that is not the best way to end a
script.


It is the only way in a client-side script in a UA. You could only wrap
all the global code in single global method, call that method from global
execution context and use the `return' statement to leave the local
execution context. But that would be not different to what I wrote:
Execution then stops because after the method call no statement follows
in global execution context.
> and continue loading the current page because the page is already
> loading as designed.


See above. Redirection will not be performed if the document is part
of a frameset.


Hmmm... I don't think that is true.


Does not matter here what you may think. It will not be performed then
because parent != top; the condition will evaluate to `false', so the
`&&' operation will evaluate to `false' and so the parameter of the `if'
statement will evaluate to `false' which will cause the bytecode
interpreter to skip the compiled block that follows:

| [...]
| if (typeof parent != "undefined"
| && typeof top != "undefined"
| && parent == top // not in a frameset
| && typeof location != "undefined")
| {
| // [redirection]
| }
| [no further statement or expression]
I struggled with a piece of code recently that required redirection of
frames.
But not with my piece of code.
At one point, it seemed to work, but I never got any consistent behavior
so I ended up scrapping it.
You may want to post it (or a link to it if it's too big) here for a more
competent evaluation.
Also, it is possible to refresh the top frame even if not the
subordinates. Of course all the frames can be loaded/reloaded
in response to events, so no major loss if redirection doesn't work.


I don't know what this has to do with the task discussed.
> Expect the unexpected.

Learn how to use search engines.


Search to learn engine use why.


Back your pardon?
PointedEars
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.