admin管理员组文章数量:1221922
I'm making a website and one of pages is using a javascript file from an external site
That external js contains (in fact loads) an image that I don't need on my page.
How do I stop that image from loading (let's say the img url is ".png")? Is there any javascript code that could do it?
Thanks to all of you
I'm making a website and one of pages is using a javascript file from an external site
That external js contains (in fact loads) an image that I don't need on my page.
How do I stop that image from loading (let's say the img url is "http://image.com/img.png")? Is there any javascript code that could do it?
Thanks to all of you
Share Improve this question edited Nov 2, 2012 at 9:59 HungryCoder 7,6161 gold badge40 silver badges52 bronze badges asked Nov 2, 2012 at 9:56 AMDAMD 1,2684 gold badges15 silver badges33 bronze badges 1- possible duplicate of Prevent images from loading – Peter O. Commented Nov 2, 2012 at 11:47
5 Answers
Reset to default 5Once DOM is loaded stop downloading any of resources (window.stop()
for modern browsers, document.execCommand("Stop", false)
for IE). Then find resources you are required in and ask browser to download them.
You can select the image with the URL and remove it from DOM
$(document).ready(function(){
$('img[src="http://image.com/img.png"]').remove();
});
Update 1:
As you probably wanted partial matching, try this one:
$(document).ready(function(){
$('img[src*="image.com/img.png"]').remove();
});
You cannot cancel a download of a single resource with JavaScript. As @nkamm answered, you could invoke the window.stop()
method, but then it stops downloading any of resources (like pressing the "stop" button in the browser).
If the image you try to stop downloading will not be rendered in the DOM, it worth cancel all other downloads in your page?
The only way to have full control over it is WebWorkers.
Basically you create a xhr.worker.js that has only a HTTP request to load the image (receives the URL via a message).
After he loads the image, the image will be in the browsers cache so you just need to set the src='' to the image URL to show it.
If you kill the specific webworker, the image loading will be canceled too !
$(window).load(function(){
$('img[src="http://image.com/img.png"]').remove();
});
本文标签: jqueryJavascript stop image loadingStack Overflow
版权声明:本文标题:jquery - Javascript stop image loading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739376252a2160497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论