Reputation: 1303
I'm trying to get the dimensions of a image without to have it in DOM.
var myVariable;
$('<img src="http://www.skrenta.com/images/stackoverflow.jpg"/>').load(function(){
myVariable = this.width;
});
console.log(myVariable);
Why console show that myVarible is undefined ?
Upvotes: 1
Views: 68
Reputation: 160311
Image loading happens in separate requests.
myVariable
will be set to the width at an arbitrary time in the future, almost always after that console.log
call happens.
Upvotes: 1
Reputation: 19617
Your variable is undefined, because it's initialized after image is loading, or in another words: console.log
calls early then function in load
.
Upvotes: 3