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
    Regular Coder
    Join Date
    Jun 2010
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Old Pedant View Post
    And his "gracefully degrade" argument is silly. If JavaScript is not enabled in a given browser, the net effect of using the inline onclick vs. using setAttribute (and, indeed, vs. using any means of attaching a handler) is no difference at all: in all cases, nothing happens in the browser, at all, when the link is clicked. "Degrades", yes. "Gracefully"? Not really.
    You're wrong. If you set an onclick on an element using setAttribute, but on the anchor tag, you have an href, you can stop the page from it's default behavior and get the href value and apply it to the other images. If there is no javascript enabled, when the user clicks the anchor that is linked to the image, it will just load the image in a new page.

    So this does degrade, and it does so GRACEFULLY!
    For more tutorials on my please check out my youtube channel here

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Okay, you win that round in the sense that indeed it does *something*. Though of course you neglected to say anything about changing the HREF in your prior messages.

    But it's highly unlikely that it does what he wants it to do.

    Namely, it moves the user away from that (presumably important) page to a page displaying nothing but an image.

    I suppose that, if I were going to use that as a (not so graceful, in my opinion only) fallback, then I'd also add a target="_blank" or similar to the <a> so that at least the current important page is not (normally) lost.

    But the point about setAttribute vs. attaching an event still stands.
    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
    Regular Coder
    Join Date
    Jun 2010
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, you win that round in the sense that indeed it does *something*. Though of course you neglected to say anything about changing the HREF in your prior messages.

    But it's highly unlikely that it does what he wants it to do.

    Namely, it moves the user away from that (presumably important) page to a page displaying nothing but an image.

    I suppose that, if I were going to use that as a (not so graceful, in my opinion only) fallback, then I'd also add a target="_blank" or similar to the <a> so that at least the current important page is not (normally) lost.

    But the point about setAttribute vs. attaching an event still stands.
    Here you go.


    Code:
    
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    
    <img src="images/image1.jpg" alt="image 1" />
    <img src="images/image2.jpg" alt="image 2" />
    <a href="images/image3.jpg" id="third">Third Image</a>
    
    <script>
    
    var third=document.getElementById('third');
    var images=document.getElementsByTagName('img');
    
    third.onclick=function(){
    	
         var theHref=this.href;
    	 
    	 for(var i=0; i < images.length; i++){
    		 images[i].setAttribute('src',theHref);
    	 }
    	 
    	 return false;
    }
    
    </script>
    </body>
    </html>
    the code isn't hard, and it degrades gracefully. writing onclick="thisfunction(); return false;" is just as easy and hard as writing variable.onclick=function(){}
    For more tutorials on my please check out my youtube channel here

  4. #19
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by pdiddles03 View Post
    the code isn't hard, and it degrades gracefully. writing onclick="thisfunction(); return false;" is just as easy and hard as writing variable.onclick=function(){}
    But it does not work in <=IE8. So almost useless IMO. My code works in any browser.

    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.

  5. #20
    Regular Coder
    Join Date
    Jun 2010
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    But it does not work in <=IE8. So almost useless IMO. My code works in any browser.
    Seriously, do you not read previous posts?

    According to the w3schools, only 4.7 percent of people use IE8 as of this month
    Last edited by pdiddles03; 09-20-2013 at 01:12 PM. Reason: blah
    For more tutorials on my please check out my youtube channel here

  6. #21
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by pdiddles03 View Post
    Seriously, do you not read previous posts?

    According to the w3schools, only 4.7 percent of people use IE8 as of this month
    So? That means your code only works for 95.3% of users. Mine works for 100% of users.

    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.

  7. #22
    Regular Coder
    Join Date
    Jun 2010
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    So? That means your code only works for 95.3% of users. Mine works for 100% of users.
    That's nice.
    For more tutorials on my please check out my youtube channel here


 
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
  •