Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript how to set attributes for images only in IE 8 and below?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #159149
    raj080288
    Participant

    Hi,

    I am using a plugin called mobify.js to help with my responsive images. However, to avoid blocking the javascript, the documentation suggests adding a ‘x’ to the image source so it would be displayed as <img x-src="..." />.

    This does not render or work in IE 8 and below so I need a way of changing the image tag back to <img src="..."'> for IE 8 and below only.

    So far I have tried the code below but IE is not recognizing it.

    //sets the image source equal to the x-src element.setAttribute(‘src’,element.getAttribute(‘x-src’)); //removes x-src element.removeAttribute(‘x-src’);

    Thanks in advance.

    Raj

    #159195
    raj080288
    Participant

    Hi, can someone help please?

    Thanks

    #159214
    James
    Participant

    EDIT: Solution misinterpreted.

    #159217
    raj080288
    Participant

    Hi James,

    I totally understand what you said and am aware of how to create stylesheets to target IE however, I need the code that can chance the <img src="x-img"> back to the normal <img src="img"> ONLY for ie which is 8 and lower.

    Hope this makes sense?

    Raj

    #159218
    Senff
    Participant

    Create a jQuery file that contains something like this (untested):

    $(document).ready(function(){
    
        $('img').each(function(i) {
            srcvalue = $(this).attr('x-src');
            $(this).attr('src',srcvalue).removeAttr('x-src');
        });
    
    });
    

    Then only load it for IE8 and lower by putting something like this in the HEAD (untested):

    <!--[if lt IE 9]>
         <script src="thatIE8script.js" type="text/javascript"></script>
    <![endif]-->
    
    #159324
    raj080288
    Participant

    Hi Senff,

    Thanks for the post, and sorry for the late reply been busy over the Christmas period!. That solution would only remove the <img x-src> code, I need a code so it is also replaced back to the original <img src>.

    I am also looking for a javascript work around if possible. Tried so many things but just not working!!!

    Have a good new year anyway!

    Thanks,

    Raj

    #159326
    Senff
    Participant

    My solution IS a Javascript solution and should remove the x-src, as well as add the src. It should change <img x-src=”blabla.gif> to <img src=”blabla.gif>.

    Can you show where you implemented it perhaps so I can check why it doesn’t work?

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.