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


Groups > comp.lang.javascript > #30146

Re: onload and onclick

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: onload and onclick
Date 2016-03-29 00:57 +0200
Organization albasani.net
Message-ID <ndcct2$9p7$1@news.albasani.net> (permalink)
References <ndb6qc$evs$1@gioia.aioe.org> <ndc59k$s7s$2@news.albasani.net> <3360887.gmJMtLSuvG@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> - If you include this script after the element with the ID "start", you
>>   don't have to wait for the "load" event before you assign a click
>>   handler to the element.
> 
> Not guaranteed:
> 
> <https://www.w3.org/TR/2014/REC-html5-20141028/introduction.html#common-pitfalls-to-avoid-when-using-the-scripting-apis>

That specific section you linked to actually supports what I wrote:

| On the other hand, parsing of HTML files happens asynchronously and
| incrementally, meaning that the parser can pause at any point to let
| scripts run.

The parser, having parsed and created the "start" element, pauses to let
the script add a "click" listener to it. There is _no_ potential for a
race condition as with "load", which is what that part of the spec warns
about.

Adding click listeners before the DOMContentLoaded stage has always
worked just fine.

>> - Strict mode makes no difference in this particular function, so the
>>   "use strict" statement could be left out.
> 
> Questionable advice.

This was _not_ advice. Learn to read.

> If the function is later augmented with source code to which strict
> mode would be relevant

Sure, and if my grandmother had wheels she'd be a wagon.

I said "this particular function". If you modify the code, the statement
no longer applies to it.

>> - Explicitly referring to the `window` object is optional;
> 
> Cite evidence.

No, do your own homework.

>> you could write `onload = function ()...` with the same effect.
> 
> A Really Bad Idea.  What if there happens to be a property of an object or a 
> variable in the scope chain that is not the targeted object?

We actually have the full code in this case and can easily see that
there is no such custom object or property. Creating one would be a
Really Bad Idea, indeed. If you're actually going to assume that
`onload` is somehow in danger of being something other than intended,
then `window` is affected in exactly the same way.

> What if it is strict mode code?

1) Nothing. Not a problem.

2) We know the code, we're not in global strict mode.

> This change makes the code less compatible and harder to 
> reuse *at no advantage*.

Hence:

>> Personally, I wouldn't use any of these.

...
>>   window.addEventListener("load", function () {
>>       // etc
>>   });
>>
>> This allows for more than one listener per target and event type, which
>> is particularly useful for the "load" event.
> 
> Yes, the event listener should be added.  But verbatim this does not work in 
> IE < 9, and IE 9 in Compatibility Mode.  So if that is a problem a wrapper 
> is required.  (Microsoft has terminated support for versions before IE *11* 
> in January. [1]  We have removed IE 8 support last year [IIRC], and are 
> going to stop actively supporting IE 9 sometime this year.)

So, you no longer support IE8, but I should? Anyway, this is easily
fixed by using an additional `false` argument. Libraries generally take
care of that, too.


- stefan

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


Thread

onload and onclick emf <emfril@gmail.com> - 2016-03-28 08:07 -0400
  Re: onload and onclick Stefan Weiss <krewecherl@gmail.com> - 2016-03-28 22:47 +0200
    Re: onload and onclick Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-28 23:23 +0200
      Re: onload and onclick Frank Kozuschnik <franko@nurfuerspam.de> - 2016-03-29 00:20 +0200
        Re: onload and onclick Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-29 00:43 +0200
      Re: onload and onclick Stefan Weiss <krewecherl@gmail.com> - 2016-03-29 00:57 +0200
        Re: onload and onclick Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-29 02:04 +0200
          Re: onload and onclick Stefan Weiss <krewecherl@gmail.com> - 2016-03-29 04:47 +0200
            Re: onload and onclick Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-31 20:06 +0200
              Re: onload and onclick Aleksandro <aleksandro@gmx.com> - 2016-03-31 15:37 -0300
              Re: onload and onclick Stefan Weiss <krewecherl@gmail.com> - 2016-04-02 01:43 +0200
    Re: onload and onclick Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-29 00:33 +0200
    Re: onload and onclick Stefan Weiss <krewecherl@gmail.com> - 2016-03-29 05:03 +0200
    Re: onload and onclick emf <emfril@gmail.com> - 2016-03-29 01:45 -0400
  Re: onload and onclick Aleksandro <aleksandro@gmx.com> - 2016-03-28 18:33 -0300

csiph-web