Stop loading of images on a hashchange event via JavaScript or jQuery

北城余情 提交于 2019-12-04 09:26:26

I tested this solution in WebKit (Chrome/Safari), Firefox and IE and it seems to work.

I used a function that uses window.stop(); for most browsers, and IE's own way of doing it if the browser doesn't support window.stop()

See more here: https://developer.mozilla.org/en/DOM/window.stop

So, for browsers that support it (Firefox, WebKit) use:

window.stop();

For IE use:

document.execCommand("Stop", false);

However be careful ...

This is basically like pressing the "stop" button in the browser so everything currently being downloaded will stop, not just images (like I wanted.)

So, inside ...

$(window).bind('hashchange', function () {

});

Here is the function I used ...

function stopDownloads() {
    if (window.stop !== undefined) {
        window.stop();
    }
    else if (document.execCommand !== undefined) {
        document.execCommand("Stop", false);
    }   
}
Jamie Wong

I'm fairly confident this isn't possible (but would be happy to be proved wrong).

The reason being is that images embedded in your website result in HTTP GET requests being sent to your server. Once that request is sent, your server is going to process and respond to that request, which will get handled by the browser.

Now, if it is possible, I would guess it would involve either going through each image and nulling out its src attribute (which I don't think will work because, as I said, the requests have already been sent). Either that or calling some (likely browser dependent) mechanism which halts image loading.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!