473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

When is window.location = "url" executed?

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
10 16520
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
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
>> 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
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
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

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
It is Javascript, I tellYou. :-)

http://www.askblax.com

Jul 23 '05 #8
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: NotGiven | last post by:
I changed my POST to GET and find appended to the url, the above string. Why and what can I do to NOT have them appended? Thanks.
6
by: Harald Weiser | last post by:
Hi you out there. I use the following string to go back to a page that's in the history. <A HREF="javascript:history.go('dosearch=0')"> But nothing happens. Using the complete URL makes no...
4
by: | last post by:
Some time ago I installed VC# 2003, made a small generic project, compile with the allow unsafe flag and I get the error below: "error CS1577: Assembly generation failed -- Unexpected exception...
5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
2
by: Luke | last post by:
Hi I have the following code which is an ASP questionnaire using an Access database. (I am using access as I have no choice!). Basically there is an html form which submits the form to the page...
3
by: VB Programmer | last post by:
When I run my asp.net app locally it runs perfectly. I upload it to another server it runs perfectly. I upload it to a 2nd server and the 1st page (Default.aspx) comes up fine. I then navigate...
3
by: Willy | last post by:
I have a website that uses querystrings on a certain page to show multiple contents. I have created mapped pages to hide the gory details of these querystrings. So instead of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.