469,303 Members | 1,900 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,303 developers. It's quick & easy.

Help with understanding onreadystatechange

Here is a simplified example of what I'm trying to understand.

This works fine:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}
---------------------------------------------------------------------------

This never fires onreadystatechange:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.location = sURL2
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}
---------------------------------------------------------------------------

I've worked around it by doing:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
window.close()
window.open(sURL2,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}
---------------------------------------------------------------------------

I would prefer just to navigate within the already open window (2nd
example), but just can't figure out how. Any help with this would be
extremely appreciated. Even an answer like "you can't do it the way you
want" would at least stop me from pulling my hair out.

Thanks,
Jeremy
Jul 23 '05 #1
7 12352
Lee
Jeremy Gollehon said:
This never fires onreadystatechange:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.location = sURL2
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}


That code will open a new window (which it subsequently ignores)
and then changes the location of the current document.
None of the remaining code is likely to be executed because you've
just loaded new content.

Maybe that wasn't the code you had intended to post?

Jul 23 '05 #2
Lee,
Thanks for the reply.
Let me approach this a different way.

Why does this code only fire the 'loading' state of readyState? It never
gets to 'complete'.
----------------------------------------------------------------------------
---
w =
window.open('javascript:document.location.replace( "http://www.google.com")')

w.document.onreadystatechange = function()
{
alert(w.document.readyState)
}
----------------------------------------------------------------------------
----

Thanks again,
Jeremy
Lee wrote:
Jeremy Gollehon said:
This never fires onreadystatechange:
------------------------------------------------------------------------- -- window.open(sURL1,'Window')
document.location = sURL2
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}


That code will open a new window (which it subsequently ignores)
and then changes the location of the current document.
None of the remaining code is likely to be executed because you've
just loaded new content.

Maybe that wasn't the code you had intended to post?



Jul 23 '05 #3
In article <10*************@corp.supernews.com>,
"Jeremy Gollehon" <j_****************@hotmail.com> wrote:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}

What is the difference between this and the onload handler?

Robert
Jul 23 '05 #4
I don't have control of the onload handler of the page.
The script I'm working on is client side from a browser (Maxthon) button
plugin. It's basically like writing a bookmarklet without the size
restrictions.

Basically, what I'm trying to accomplish is knowing when a page is fully
loaded. I've been able to do it with the code you quoted below on a new
window, but can't figure out a way to determine if a page has completed
loading when you just navigate within an already open window (tab in
Maxthon's case). The onreadystatechange function doesn't seem to fire when
using document.location.

Maybe there's a way to inject code into the onload handler of a page before
it's done loading? I'm new to javascript and kind of stabbing in the dark.
Thanks for any help you can provide.

-Jeremy
Robert wrote:
In article <10*************@corp.supernews.com>,
"Jeremy Gollehon" <j_****************@hotmail.com> wrote:
------------------------------------------------------------------------- -- window.open(sURL1,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}

What is the difference between this and the onload handler?

Robert

Jul 23 '05 #5
Jeremy Gollehon wrote:
This works fine:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}
---------------------------------------------------------------------------
This never fires onreadystatechange:
---------------------------------------------------------------------------
window.open(sURL1,'Window')
document.location = sURL2
This navigates you off the current page, everything on the current document,
including the script that follows, is destroyed either immediately, or at some
undetermined (small) amount of time after you issue the above assignment.
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}


--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
Lee
Jeremy Gollehon said:

Lee,
Thanks for the reply.
Let me approach this a different way.

Why does this code only fire the 'loading' state of readyState? It never
gets to 'complete'.
----------------------------------------------------------------------------
---
w =
window.open('javascript:document.location.replace ("http://www.google.com")')

w.document.onreadystatechange = function()
{
alert(w.document.readyState)
}


It shows that it is loading immediately, but when the new
content (google) is loaded into the window, it clears the
existing onreadystatechange function, so you never hear
from it again. Loading new content clears all functions.

Jul 23 '05 #7
Lee and Grant,
Your explanations make perfect sense. Thanks for helping out a newbie.

-Jeremy
Grant Wagner wrote:
Jeremy Gollehon wrote:
This works fine:
------------------------------------------------------------------------- -- window.open(sURL1,'Window')
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}
------------------------------------------------------------------------- -- This never fires onreadystatechange:
------------------------------------------------------------------------- -- window.open(sURL1,'Window')
document.location = sURL2


This navigates you off the current page, everything on the current
document, including the script that follows, is destroyed either
immediately, or at some undetermined (small) amount of time after you
issue the above assignment.
document.onreadystatechange = function()
{
if (document.readyState == 'complete')
{
alert('yo')
}
}

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Krzysztof Kubiak | last post: by
5 posts views Thread by Jason Morehouse | last post: by
3 posts views Thread by Andrew Poulos | last post: by
1 post views Thread by akhil.patel | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.