admin管理员组文章数量:1299991
Chrome is wrongly reporting width and height values for images during, or just after, load time. Jquery is used in this code example:
<img id='image01' alt='picture that is 145x134' src='/images/picture.jpg' />
<script>
var img = $( 'img#image01' )
img.width() // would return 145 in Firefox and 0 in Chrome.
img.height() // would return 134 in Firefox and 0 in Chrome.
</script>
If you put the script in a $(function(){})
function, the result is the same. but if you run the code a few seconds after the page has loaded, chrome returns the correct result.
<script>
function example () {
var img = $( 'img#image01' );
img.width() // returns 145 in both Firefox and Chrome.
img.height() // returns 134 in both Firefox and Chrome.
}
window.setTimeout( example, 1000 )
</script>
Also if you specify the width and height values in the img tag, the script seems to work as expected in both Firefox and Chrome.
<img id='image01' src='/images/picture.jpg' width=145 height=134 />
But as you cannot always control the html input, this is not an ideal workaround. Can jQuery be patched with a better workaround for this problem? or will I need to specify the width and height for every image in my code?
Chrome is wrongly reporting width and height values for images during, or just after, load time. Jquery is used in this code example:
<img id='image01' alt='picture that is 145x134' src='/images/picture.jpg' />
<script>
var img = $( 'img#image01' )
img.width() // would return 145 in Firefox and 0 in Chrome.
img.height() // would return 134 in Firefox and 0 in Chrome.
</script>
If you put the script in a $(function(){})
function, the result is the same. but if you run the code a few seconds after the page has loaded, chrome returns the correct result.
<script>
function example () {
var img = $( 'img#image01' );
img.width() // returns 145 in both Firefox and Chrome.
img.height() // returns 134 in both Firefox and Chrome.
}
window.setTimeout( example, 1000 )
</script>
Also if you specify the width and height values in the img tag, the script seems to work as expected in both Firefox and Chrome.
<img id='image01' src='/images/picture.jpg' width=145 height=134 />
But as you cannot always control the html input, this is not an ideal workaround. Can jQuery be patched with a better workaround for this problem? or will I need to specify the width and height for every image in my code?
Share Improve this question edited Apr 10, 2020 at 2:28 Kevin Workman 42.2k12 gold badges71 silver badges110 bronze badges asked Jun 14, 2010 at 7:38 StefanStefan 9,5998 gold badges39 silver badges46 bronze badges 1- 1 Now that's what sponsored tags are for! – beggs Commented Jun 14, 2010 at 8:07
4 Answers
Reset to default 9Your quoted code doing it inline is getting a reasonable result, as the image may well not be loaded before your code executes. You said that you put it in an onload
handler and that also has the same result. Did you really mean an onload
handler? Because I cannot replicate that result. Note that the jQuery ready
function is not an onload
handler, it happens much earlier than that. To ensure images are loaded, use the image's load
event or the window
load
event. jQuery ready
happens a lot earlier than that.
So this should work:
$(window).bind('load', function() {
var img = $("#theimage");
log("Width: " + img.width());
log("Height: " + img.height());
});
...(where log
is something that outputs to the page), and this should work:
$(document).ready(function() {
$("#theimage").bind('load', function() { // BUT SEE NOTE BELOW
var img = $(this);
log("Width: " + img.width());
log("Height: " + img.height());
});
});
...but this will not:
$(document).ready(function() {
var img = $("#theimage");
log("Width: " + img.width());
log("Height: " + img.height());
});
...because it happens too soon.
Edit: I really, really should have said in the above: If you're looking the image's load
event (rather than the window
load event), you need to check to make sure the image hasn't already been loaded, because its load
event may have already fired. That would look something like this:
$(document).ready(function() {
var img, imgElement;
// Find the image
img = $("#theimage");
imgElement = img[0];
if (imgElement && imgElement.plete) {
// Already loaded, call the handler directly
loadHandler.call(imgElement);
}
else {
// Not loaded yet, hook the event
img.bind('load', loadHandler);
}
function loadHandler() {
var img = $(this);
log("Width: " + img.width());
log("Height: " + img.height());
img.unbind('load', loadHandler);
}
});
That will call loadHandler
at the earliest opportunity, whether the image load won the race or the ready
code won the race.
This is not an error. The img
element is 0x0 wide until the image is loaded. You should wait until the image is entirely loaded. Try something like this:
$("img#image01").load(function(){
//do things here
});
$('#img2').show(function() {
if( $.browser.safari ){
alert ( $('#img2').width() );
}
});
.load does not work for Chrome dimension so use .show ;)
Did you try to put that code into
$(document).ready(function(){
});
if that doesn't help, use jQuerys .load() function
var $img = $('#image01'),
iwidth = 0,
iheight = 0;
$(document).ready(function(){
$img.load(function(){
var img = $('#image01');
img.removeAttr('width').removeAttr('height').css({width: '', height: ''});
iwidth = this.width;
iheight = this.height;
});
});
$img[0].src = '';
$img[0].src = $img[0].src;
本文标签: javascriptGoogle Chrome incorrectly reporting width and height for images during loadStack Overflow
版权声明:本文标题:javascript - Google Chrome incorrectly reporting width and height for images during load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741605183a2387934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论