1

Is it possible to remove src attribute for each img tag before image is fetched ? I want to change image fetching behavior.

What I did:

$('img').each(function() {
    var elem = $(this);
    elem.removeAttr('src');
    // ... my custom actions
});

What happened: Image is fetched, before src attribute is removed :/

Thanks

4
  • Have your tried to accomplish this on your own yet? Please post specific code and any issues you are having. Commented Feb 26, 2019 at 14:13
  • I updated my post, I used jQuery to remove attribute Commented Feb 26, 2019 at 14:17
  • Possible duplicate of Changing HTML Data Before Page Rendering Commented Feb 26, 2019 at 14:20
  • Thanks, I will try it Commented Feb 26, 2019 at 14:24

1 Answer 1

2

If your src is in the markup then it will fetch before your js execute. Simply don't use src attribute, put the src in say data-src, then grab it with js and do what you want to do.

Sign up to request clarification or add additional context in comments.

2 Comments

This is i want to avoid because the project I am working on is very big. But I realized that there is no solution except changing src attributes to data-* manually. Thanks for your response :)
If you are worried about SEO, you could also use an anchor tag with the href pointing to the image and then use js to change them into image tags. This way you will have better seo because the image will be indexed and you are not loading them on load. In our own project, we use a div tag with all the info needed in data attributes then output the right image depending on screen size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.