admin管理员组文章数量:1327674
I did a lot of research about this problem, but with no success.
Essentially what I want to do is this:
1) Replace the src- attribute of all images with a placeholder, like 'blank.gif'
2) Add the HTML5 data-original attribute with the original image location
3) Lazyload the images (it needs the data-original attribute to work properly)
What I tried without success:
1) attaching this eventlistener document.addEventListener('beforeload', doBeforeLoad, true);
with this function
function beforeload() {
var blank = 'image/location/images/blank.gif';
$('img').each(function() {
var orig = $(this).attr('src');
$(this).attr('data-original',orig);
$(this).attr('src',blank);
console.log("changing all data on images");
});
}
2) on document.ready sure it won't work..
I have no idea if this is even possible, so any help | suggestion | resource would be greatly appreciated
PS: for example I want to make it work here) (because it's a image-heavy article )
I did a lot of research about this problem, but with no success.
Essentially what I want to do is this:
1) Replace the src- attribute of all images with a placeholder, like 'blank.gif'
2) Add the HTML5 data-original attribute with the original image location
3) Lazyload the images (it needs the data-original attribute to work properly)
What I tried without success:
1) attaching this eventlistener document.addEventListener('beforeload', doBeforeLoad, true);
with this function
function beforeload() {
var blank = 'image/location/images/blank.gif';
$('img').each(function() {
var orig = $(this).attr('src');
$(this).attr('data-original',orig);
$(this).attr('src',blank);
console.log("changing all data on images");
});
}
2) on document.ready sure it won't work..
I have no idea if this is even possible, so any help | suggestion | resource would be greatly appreciated
PS: for example I want to make it work here) (because it's a image-heavy article )
-
1
too late to stop requests using javascript if
img
tags already in html. Parse them with server code to do the same before html is sent – charlietfl Commented Jan 19, 2013 at 13:54 - Ok, thanks, thought jquery could manage this.. – christian_fei Commented Jan 19, 2013 at 14:18
3 Answers
Reset to default 3The browser will start making the requests before you can start executing your JS. I remend you alter the source html to be in the data
pattern you require for lazy loading the images. This needs to happen before the browser receives it. This shouldn't be too much trouble if it's server generated.
It sounds like you're looking to do what amounts to adding an img
and yet you're approaching it through the idea of modifying an image. If you use a placeholder span
or something similar then you can avoid the whole question and have less variables to consider. Create a span with the dataset for the information that you want, and optionally a class
which attaches a background image to indicate that the image hasn't been loaded, and then on page load find the appropriate span
s insert the img
element (or replace the span
if preferred), and do any other cleanup such as removing the "loading" style class
. This could allow for structuring your page in a way that is less likely to ever be in an inconsistent state.
That technique was used in older browsers with luck, but now modern browsers are not longer cheated with this way, so you have to do blank src and data source attribute on server side, here is popular jQuery lazy load plugin homepage, http://www.appelsiini/projects/lazyload you bet they did a lot of researches before writing this
Latest version of Lazy Load is not a drop in replacement to your webpage. New browsers load image even if you remove the src attribute with JavaScript. Now you must alter your html code. Put placeholder image into src attribute of your img tag. Real image url should be stored data-original attribute.
UPD: just think over a bit and probably better approach to data- attribute would be using noscirpt
wrapping for images you do want loaded lazlily here roughly illustration for idea http://jsbin./uqomeb/2/edit I would possibly do simple jQuery plugin later based on this
本文标签: javascriptChange src of image before request has been sentStack Overflow
版权声明:本文标题:javascript - Change src of image before request has been sent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223606a2435626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论