{"id":928,"date":"2012-06-19T17:12:14","date_gmt":"2012-06-19T17:12:14","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=928"},"modified":"2012-06-19T17:12:14","modified_gmt":"2012-06-19T17:12:14","slug":"javascript-get-image-dimensions-get-image-width-height","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/javascript-get-image-dimensions-get-image-width-height\/","title":{"rendered":"JavaScript Get Image Dimensions (Width &#038; Height) Examples"},"content":{"rendered":"<p>Have you ever wanted to get image dimensions using JavaScript? In this article, I am going to share an easy way to use\u00a0<em><strong>JavaScript to get image dimensions (width &amp; height)<\/strong>.<\/em><!--more--><\/p>\n<h2>How to use JavaScript to Get Image Dimensions (Width &amp; Height)<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=928\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Following is the complete HTML + JavaScript source code that shows how to get the image dimensions (width &amp; height) when the page is loaded:<\/p>\n<h3>Example: Get Image Width &amp; Height (dimensions) using JavaScript<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\n&lt;title&gt;Javascript Get Image Dimensions&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;img src=\"images\/banner.png\" id=\"my_img\" \/&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\nvar img = document.getElementById('my_img');\n\nvar width = img.clientWidth;\n\nvar height = img.clientHeight;\n\nalert( 'Width: ' + width + ' Height:' + height );\n\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>The given code assumes that there is an image with ID &#8220;my_img&#8221; and the dimensions of this image needs to be found. When the above code is executed, the page will alert the width and height of the image.<\/p>\n<h4>Note<\/h4>\n<p>The image has to be completely loaded in order to get it&#8217;s size, or the width &amp; height values are incorrectly shown as zero.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=928\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to <em><strong>find image width and height using JavaScript<\/strong><\/em>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to get image dimensions using JavaScript? In this article, I am going to share an easy way to use\u00a0JavaScript to get image dimensions (width &amp; height).<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17,13],"tags":[80,81,35],"class_list":["post-928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tips-and-tricks","category-tips-and-tricks","tag-javascript-demos-2","tag-javascript-get-image-dimensions","tag-javascript-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-eY","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=928"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}