Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: simple javascript code does not run, no matter what browser

  1. #11
    Join Date
    Feb 2018
    Location
    germany
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    @jscheuer, soemthing else, I'm postiv surprised for now, that a mod came around and clear some here. I cant send a pm, so keep it short.

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The main difference about what's being done in the video is that it takes into account Internet Explorer 8 and less - those browser's inability to follow the standard event model. Virtually no one uses those browsers, so their proprietary attachEvent method no longer needs to be a part of any modern code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I can go a little further. If you do want make this code backward compatible with older IE versions (all the way to IE 5), you can do this:

    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Add Event Listener Demo</title>
            <meta charset="UTF-8">        
            <script>
    		function addListeners(){
    			if(window.addEventListener){
    				document.getElementById('mybtn').addEventListener("click", btn1func, false);
    			} else if(window.attachEvent){
    				var el = document.getElementById('mybtn'),
    				oldbtn1func = btn1func;
    				btn1func = function(){oldbtn1func.apply(el);};
    				el.attachEvent('onclick', btn1func);
    			}
    		}                     
    		function btn1func(){
    			alert(this.id + " : mouse-click makes script run" );
    		}
    		if(window.addEventListener){
    			window.addEventListener('load', addListeners, false);
    		} else if(window.attachEvent){
    			window.attachEvent('onload', addListeners);
    		}
            </script>
        </head>
        <body>
            <button id="mybtn">MyButton</button>
        </body>
    </html>
    But it's entirely unnecessary unless you need to support these older browsers, which, as I've said, virtually no one uses any longer. If you want to be useful in all sorts of environments, it can be good to know how to cater to antiquated software/machines (it's the older machines that are limited sometimes to run older software). This is only critical if your target audience is in a (internet technology speaking sense) backward country or organization.
    Last edited by jscheuer1; 02-09-2018 at 10:15 PM. Reason: minor code improvements
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #14
    Join Date
    Feb 2018
    Location
    germany
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    well, I dont know what to tell, thanks of course.
    Hope thats the reason for all the other exmples from which I picked up the code and nothing happend.
    Is there any way to proof that? Lets say maybe with another IDE?

  5. #15
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Integrated development environment? Yes, that would do it if it included those earlier browsers. But one can use IE 11 and it's developer console's emulation feature to see what earlier IE versions would make of code. There are some limitations. But it's mostly accurate. I have the advantage of previous experience, so you can 99% trust what I just told you. To be 100% sure you would have to find a way to test in the actual browsers.

    Something I would add, even for people who are limited by their machines as to what IE version they can run, those people almost always have the option of running an older version of Netscape and/or Firefox, both of which adopted the standard event model long before IE did. Because of that, and the fact that very few people are so limited, I tend to advise against trying to be so backward compatible unless you know for a fact that's necessary. That said, there are times when your target audience will be limited to both an older machine and an older version of IE. That's increasingly rare, but it still could at least theoretically happen. For general purposes though, just sticking to the standard event model* is more than adequate.

    *Standard Event Model (as previously posted):
    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Add Event Listener Demo</title>
            <meta charset="UTF-8">        
            <script>
    		function addListeners(){               
    			document.getElementById('mybtn').addEventListener("click", btn1func, false);                    
    		}                     
    		function btn1func(){
    			alert(this.id + " : mouse-click makes script run" );
    		}
    		window.addEventListener('load', addListeners, false);
            </script>
        </head>
        <body>
            <button id="mybtn">MyButton</button>
        </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #16
    Join Date
    Feb 2018
    Location
    germany
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have the advantage of previous experience, so you can 99% trust
    I have no doubt, but I dont think you want to ask me here all the time I have tried any new youtube example.

    I'm not an english native, I've read your answer many times, would straight agree that I missanderstand any of it, but cant see what it is.

  7. #17
    Join Date
    Feb 2018
    Location
    germany
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    some other videos, there are more, none of the code shows any reaction.
    https://www.youtube.com/watch?v=NO-U0vP5ikA
    https://www.youtube.com/watch?v=AnOAZsezLEg

    Have you ever heard about the krueger Effekt? Some kind of proof like, why stupid people easier belive themselfs ****ing smart
    Last edited by jscheuer1; 02-15-2018 at 07:25 PM. Reason: format

Similar Threads

  1. Turn simple javascript code to a button
    By JRR in forum JavaScript
    Replies: 79
    Last Post: 03-12-2018, 03:53 PM
  2. Please write a simple javascript code for me
    By laperashvili in forum JavaScript
    Replies: 2
    Last Post: 06-18-2012, 03:10 PM
  3. Replies: 1
    Last Post: 02-22-2012, 05:38 PM
  4. Replies: 3
    Last Post: 08-19-2008, 09:53 AM
  5. Looking for a simple Javascript code for a web board
    By donelian in forum Looking for such a script or service
    Replies: 1
    Last Post: 08-15-2007, 10:24 PM

Bookmarks

Posting Permissions

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