2

I am changing src of all images to '' using mutation observer. But browser is making the request to that image URL. How to block image requests from the browser?

Below is the code.

const observer = new MutationObserver(mutations => {
    mutations.forEach(({
        addedNodes
    }) => {
        addedNodes.forEach(node => {
            if( node.tagName == "IMG" ){
                node.setAttribute("data-src",src);
                node.removeAttribute('src');
            }                                      
        });
    });
});
observer.observe(document.documentElement, {
    childList: true,
    subtree: true
});
5
  • 1
    Where is your question? Commented Jan 31, 2020 at 7:45
  • 1
    Does this answer your question? Request Blocking Using Javascript? Commented Jan 31, 2020 at 7:45
  • 1
    Now check my question once. Commented Jan 31, 2020 at 7:48
  • I'm pretty sure that any markup like this: <img src="[IMG]" /> is going to trigger a request. You need to start with <img data-src="[IMG]" /> and then introduce src rather than vice versa. Commented Jan 31, 2020 at 9:15
  • we don't have option to set data-src in html, as we don't have control over html code. We are creating a third party script to block some elements from loading. Commented Jan 31, 2020 at 9:22

0

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.