Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    Sure! But those are exceptional circumstances.
    I agree but if anyone uses the forum properly and searches to see if their question is answered first then having ALL the information in the thread can be useful just in case the exception applies to the searcher's situation. I have also seen occasions where several people contribute to answering a thread and one of the contributors learns something new when another mentions something like that.

    Quote Originally Posted by Old Pedant
    LOL! Okay, okay! Point conceded!
    There are a lot of people who use JavaScript in their pages who have no idea how to write it - they simply copy scripts and follow the install instructions. Unless the script they copy has been written to be completely unobtrusive then it has the potential to clash with another script they decide to add to the same page. There are lots of script repositories where many of the scripts depend on being the only script in the page.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    Quote Originally Posted by felgall View Post
    There are lots of script repositories where many of the scripts depend on being the only script in the page.
    Way, way too many. Yes, I do concede the point. There's a difference in writing code for my use vs. writing code for people who shouldn't be allowed to use JavaScript. <grin/> Oh, okay, for people who think JavaScript is complete magic.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by felgall View Post
    There are a lot of people who use JavaScript in their pages who have no idea how to write it - they simply copy scripts and follow the install instructions. Unless the script they copy has been written to be completely unobtrusive then it has the potential to clash with another script they decide to add to the same page. There are lots of script repositories where many of the scripts depend on being the only script in the page.

    That is a very good point. But expert coders who write their own code will not run into that problem. I expect that explains why I am less fanatical than you are on avoiding global variables or event handlers. And of course the people who copy and paste do not understand the issue anyway.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  4. #19
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    I expect that explains why I am less fanatical than you are on avoiding global variables or event handlers.
    That is exactly it. Just about all of the complete scripts that I write are primarily intended to be copied and used by people who have little or no knowledge of JavaScript so the less obtrusive i can make the scripts the less likely I am to have people complaining that the script doesn't work on their page.

    The one place where I write JavaScript that isn't intended for use by people with no knowledge of JavaScript is with the tutorials that I write to help people learn JavaScript. There I also try to present the unobtrusive way of doing things because it helps them to avoid clashes in their own scripts.

    Those with a more advanced knowledge of JavaScript know in more detail what the impact is of any given approach to writing their script a particular way and can therefore use more obtrusive code safely.

    Since most of the people asking questions about JavaScript on forums are beginners it is generally safer to give them an answer that produces less obtrusive code. Even if they are not a beginner we don't know what other JavaScript they plan on adding to their page tomorrow and so a more obtrusive version that works today might break when they add tomorrow's code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  5. #20
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    We are totally in agreement.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  6. #21
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I like conceptions of learning through online discussion.
    Thank you again.

    Can anybody explain function addevt(o,t,f,p)// four parameter
    And addevt(window, ‘load’,init) // three parameter
    How dose function addevt(o,t,f,p) work?

  7. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    The p is optional and represents additional parameters to be passed to the function you are adding to the event.

    So say you had
    Code:
    function init( e, name )
    {
        if ( e == null ) { e = window.event; }
        var foo = name;
        ...
    }
    Then you could code:
    Code:
    addevt( window, "load", init, "zamboni" );
    This is pretty unusual usage, but I think Vic put it there for completeness.

    It does occur to me that it's pointless to try to pass e when you are using MSIE and attachEvent.

    It might be more sensible to code that as:
    Code:
    function addevt(o,t,f,p)
    {
        if (o.addEventListener)
        {
            o.addEventListener(t,function(e){ return f(e,p);}, false);
        } else if (o.attachEvent) {
            o.attachEvent('on'+t,function(){ return f(window.event,p); });
        }
    }
    and then your own function never needs to check for the e argument as it will always be there.
    Code:
    function init( e, name )
    {
        // e will actually be window.event if MSIE
        var foo = name;
        ...
    }
    Vic can correct me if I'm off base.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •