Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27372

Re: XMLHttpRequest and Google Chrome

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: XMLHttpRequest and Google Chrome
Date 2015-04-14 12:43 +0000
Organization A noiseless patient Spider
Message-ID <mgj21e$e8r$2@dont-email.me> (permalink)
References (6 earlier) <mggm7e$qbf$1@dont-email.me> <mggock$rt7$1@solani.org> <mggqp5$cu6$1@dont-email.me> <130420151829307070%timstreater@greenbee.net> <mgh03o$3lk$1@dont-email.me>

Show all headers | View raw


On Mon, 13 Apr 2015 18:59:13 +0100, Robbie Brown wrote:

> if I change <head>
> 
> <script type="text/javascript">
> 	window.onload = getCart;
> </script>
> 
> The function doesn't get called, I get no "hello"
> This is the same in all my browsers. I have no idea why as you and
> others say that getCart should work, it simply doesn't. I'm not making
> it up, it doesn't execute, I'm not mistaken, so, obviously something is
> still missing.
> 
> So it's back to the drawing board and javascript console.

What you're missing is the previously explained "getCart should be a 
reference to a function".

This code:

<script type="text/javascript">
	window.onload = getCart();
</script>

runs the getCart function as soon as the parser finds it, and stores the 
return value of getCart in the onload attribute of the window object. 
When the window finishes loading, it attempts to call the stored value as 
if it were a function, which does nothing because getCart doesn't return 
a function object.

This code:

<script type="text/javascript">
	window.onload = function() {getCart();};
</script>

stores a function that calls getCart() in the onload attribute of the 
window object. When the window finishes loading, it attempts to call the 
stored value as if it were a function, which works because what has been 
stored is actually a function.

This code:

<script type="text/javascript">
        var bing = function() {getCart();};
	window.onload = bing;
</script>

stores the function object in the variable named bing, and then stores 
bing in the onload attribute of the window object. When the window 
finishes loading, it attempts to call the stored value as if it were a 
function, which works because what has been stored is actually a function.

Now, let's test these. Working code at 

www.sined.co.uk/tmp/loadfunc1.htm

www.sined.co.uk/tmp/loadfunc2.htm

www.sined.co.uk/tmp/loadfunc3.htm 

Essentially they're all calling the getCart function, but the difference 
is in when they call it.

You should notice that the call with "1" happens before the page load 
completes, ie the alert box appears over a blank page background. The 
calls with 2 and 3 should appear after the page has loaded, ie with text 
and images in the background.

When you have a function that you wish to invoke during page loading, but 
which accesses the elements of the document dom that is being loaded, it 
is essential to defer the function call until after the document has 
finished loaded.

My "normal" method of doing this is to use the following:

<script type="text/javascript">
	window.onload = function() {pageLoaded();};
</script>

and then everything else I want to do is in my pageLoaded function.

Note that multiple calls to assign different functions to window.onload 
may not do as you expect, window.onload does not store a list of 
functions to execute, it stores a single value.

If you make the calls:

<script type="text/javascript">
	window.onload = function() {getCart();};
	window.onload = function() {pageLoaded();};
	window.onload = function() {init();};
</script>

then only the init() function will be called when the page has finished 
loading. Hence my preference for having one pageLoaded() function which 
does all the other work.

Hope this helps your understanding.

p.s. The elf will probably whine I'm wrong, but if the code examples I 
posted work for you then I suggest you ignore him. He just enjoys waving 
his e-wang around and pretending he knows best. LoNg LiVe ThE jAvAsCrIpT 
lIbErAtIoN fRoNt!

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-12 19:36 +0100
  Re: XMLHttpRequest and Google Chrome JJ <jj4public@vfemail.net> - 2015-04-13 12:33 +0700
    Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 07:41 +0100
  Re: XMLHttpRequest and Google Chrome Martin Honnen <mahotrash@yahoo.de> - 2015-04-13 11:30 +0200
    Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 11:50 +0100
      Re: XMLHttpRequest and Google Chrome Tim Streater <timstreater@greenbee.net> - 2015-04-13 13:17 +0100
        Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 13:34 +0100
          Re: XMLHttpRequest and Google Chrome Tim Streater <timstreater@greenbee.net> - 2015-04-13 14:26 +0100
          Re: XMLHttpRequest and Google Chrome Martin Honnen <mahotrash@yahoo.de> - 2015-04-13 16:15 +0200
            Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 16:03 +0100
              Re: XMLHttpRequest and Google Chrome Martin Honnen <mahotrash@yahoo.de> - 2015-04-13 19:26 +0200
                Re: XMLHttpRequest and Google Chrome "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-13 20:23 +0200
          Re: XMLHttpRequest and Google Chrome Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-13 15:50 +0100
            Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 16:10 +0100
              Re: XMLHttpRequest and Google Chrome "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-13 17:46 +0200
                Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 17:28 +0100
                Re: XMLHttpRequest and Google Chrome "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-13 19:14 +0200
                Re: XMLHttpRequest and Google Chrome Tim Streater <timstreater@greenbee.net> - 2015-04-13 18:29 +0100
                Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 18:59 +0100
                Re: XMLHttpRequest and Google Chrome "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-13 20:29 +0200
                Re: XMLHttpRequest and Google Chrome Tim Streater <timstreater@greenbee.net> - 2015-04-13 19:34 +0100
                Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 20:11 +0100
                Re: XMLHttpRequest and Google Chrome "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-13 20:44 +0200
                Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 20:21 +0100
                Re: XMLHttpRequest and Google Chrome Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-14 12:43 +0000
                Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-14 14:50 +0100
                Re: XMLHttpRequest and Google Chrome Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-14 22:51 +0200
                Re: XMLHttpRequest and Google Chrome Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-13 22:28 +0200
  Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-13 19:23 +0100
    Re: XMLHttpRequest and Google Chrome Tim Streater <timstreater@greenbee.net> - 2015-04-13 19:38 +0100
  Re: XMLHttpRequest and Google Chrome "Leonardo Azpurua" <leonardo@exmvps.org> - 2015-04-13 17:36 -0430
  Re: XMLHttpRequest and Google Chrome Robbie Brown <rob@example.com> - 2015-04-14 11:35 +0100
  Re: XMLHttpRequest and Google Chrome Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-14 11:37 +0000

csiph-web