Singapore CMS Web Design

Gary Consulting Group - Singapore CMS Web Design

Timely Support and Clarity in your journey to harness the power of the internet
Email:
sales@name1price.com
Call:
+1 415 300-0019
jQuery get image height or width returns 0 problem

While developing a Content Management System based on wordpress for one of our clients we encountered a strange problem.

During the every first time when our browser loads the image and our script using the jQuery library attempts to detect the image's height and width, our script got 0 as the results for both the width and height. Below is how our script looked like.

curr_image_height = jQuery("#image_id").attr("height");
alert(curr_image_height );
//result is 0

The problem was no longer encountered during the subsequent instances the page is reloaded. However in the event whereby we cleared the cache in our browser and loaded the page contents again using the CTRL + F5 key, the same problem was once again encountered.

Our team suspected that browsers can only detect the height and widht of an image if it is already loaded in the cache. As such we remodified our codes to the following instead

jQuery("#image_id").load(function(){

	curr_image_height = jQuery(this).attr("height");
	//alert(jQuery("#"+curr_image).attr("src")+" height : "+curr_image_height );
			
})

The results were positive. By getting the jQuery library to load the image into the cache before detecting the height and width of the image, the script consistantly returns the height and width value of the image.

 
Monday, 21st Apr 2014
Content Management System powered by Gary from Name1Price.com