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


Groups > comp.lang.javascript > #30152

Re: onload and onclick

Path csiph.com!aioe.org!.POSTED!not-for-mail
From emf <emfril@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: onload and onclick
Date Tue, 29 Mar 2016 01:45:34 -0400
Organization Aioe.org NNTP Server
Lines 68
Message-ID <ndd4pt$1ge2$1@gioia.aioe.org> (permalink)
References <ndb6qc$evs$1@gioia.aioe.org> <ndc59k$s7s$2@news.albasani.net>
NNTP-Posting-Host Kxbq8cA91hl8Saz/Gf9H1g.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Thunderbird/38.7.0
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.javascript:30152

Show key headers only | View raw


On 2016-03-28 16:47, Stefan Weiss wrote:
> emf wrote:
>> window.onload = function () {
>>      "use strict";
>>      document.getElementById("start").onclick = function () {
>>          requestFullScreen();
>>          init();
>>      };
>> };
>>
>> I arrived at this solution after deciding to remove any JS code from the
>> HTML code. It works fine, however it looks a little too complicated for
>> my personal taste. Is there a simpler way to write it?
>
> This is pretty typical code for what you're doing. There are a few
> variations that could make it a little more concise, but nothing
> dramatic. For example:
>
> - 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.
> - Strict mode makes no difference in this particular function, so the
>    "use strict" statement could be left out.
> - Explicitly referring to the `window` object is optional; you could
>    write `onload = function ()...` with the same effect.
>
> Personally, I wouldn't use any of these. I would even go a step in the
> opposite direction and use the more verbose `addEventListener()` method
> instead of the DOM-0 style "onload" and "onclick" properties:
>
>    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.
>
> Most people use libraries to simplify common tasks like event handling
> and element selection. With one such library, your code might look like
> this:
>
>    $(function () {
>        $("#start").click(function () {
>            requestFullScreen();
>            init();
>        });
>    });
>
> That's shorter (and also uses the "DOMready" event). Loading a huge
> library for just a few lines of code would be counterproductive, but
> typically the library gets used for the rest of the code, as well.
>
>
> - stefan

Wow! I read through all the long thread my message generated, though, 
sincerely, I could only follow only part of it.

Yes, I'll leave my code as is for now. I had just wondered whether there 
existed a shortcut I hadn't come across.

My current modus operandi is to check my code in JSlint, and follow its 
instructions. And, of course, to consult the experts on this list for 
additional questions.

-- 
It ain't THAT, babe! - A radical reinterpretation
http://emf.neocities.org/bd/itaintmebabe.html

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