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


Groups > comp.lang.javascript > #27357

Re: XMLHttpRequest and Google Chrome

From Tim Streater <timstreater@greenbee.net>
Newsgroups comp.lang.javascript
Subject Re: XMLHttpRequest and Google Chrome
Date 2015-04-13 18:29 +0100
Message-ID <130420151829307070%timstreater@greenbee.net> (permalink)
References (4 earlier) <mggd3o$n6r$1@dont-email.me> <87h9skyte6.fsf@bsb.me.uk> <mggm7e$qbf$1@dont-email.me> <mggock$rt7$1@solani.org> <mggqp5$cu6$1@dont-email.me>

Show all headers | View raw


In article <mggqp5$cu6$1@dont-email.me>, Robbie Brown <rob@example.com>
wrote:

>On 13/04/15 16:46, Christoph M. Becker wrote:
>> Robbie Brown wrote:
>>
><snip>
>
>>> Can anyone put me out of my misery?
>>
>> The onload property of the window element is supposed to hold the
>> reference to a Function object (or null).  This function object is
>> automatically called (with a single argument, namely the respective
>> Event object[1]) after the documented has been loaded.
>>
>> In your case you're assigning the return value of getCart() to the
>> property.  The return value is undefined, and will be cast to null, so
>> no function will be called when the document has been loaded.
>>
>> Instead getCart is called right before the assignment, which causes the
>> XHR to be triggered immediately.  The rest is timing dependend.
>
>I'm starting to think it's a timing issue, maybe I've been lucky to have 
>things working so far. I still don't understand this whole getCart 
>versus getCart() thing, maybe it's something fundamental that I'm missing.

You need to assign a function to the onload property. In fact you
already do this sort of thing here:

function getReadyStateHandler(req, responseXmlHandler) {
   return function () { ... }

and here:

req.onreadystatechange = getReadyStateHandler(req, updateCart);

where your getReadyStateHandler function returns a reference to a
function (don't argue, yes it does, it says "return function ...").

In exactly the same way, this code:

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

ought to assign a function to the onload handler. The function you want
to assign is getCart, hence you need to say:

  window.onload = getCart;

This will cause getCart to be run when the onload is complete and not
before. With what you had, getCart is run as soon as:

  window.onload=getCart();

is encountered, which will likely be before the onload is complete,
meaning that <div id="cartsnapshot"> may or may not be known to the
browser at that point. So whether it works or not is down to timing.

So - notice the difference between:

  window.onload = getCart;

and:

  <body onload="getCart();">

The first requires a function reference. In the second, you put some
javascript to be executed. So you could put:

  <body onload="alert('Boo!');">

and to get the same effect with window.onload needs:

  window.onload = function () { alert('Boo!'); };

-- 
"I love the way that Microsoft follows standards. 
 In much the same manner as fish follow migrating caribou."
                                               - Paul Tomblin, ASR

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