Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One more version. No DOM manipulation needed, all characters in image file name supported.</p> <p><strong>UPDATE</strong> See an alternative version below.</p> <pre><code>var image_url = $('#something').css('background-image'), image; // Remove url() or in case of Chrome url("") image_url = image_url.match(/^url\("?(.+?)"?\)$/); if (image_url[1]) { image_url = image_url[1]; image = new Image(); // just in case it is not already loaded $(image).load(function () { alert(image.width + 'x' + image.height); }); image.src = image_url; } </code></pre> <hr> <h2>2018 solution</h2> <p>This answer is still receiving upvotes 5 years after. I thought I will share a more complete solution. This builds on top of original code and wraps it into a function. It uses jQuery Deferred Object, adds error handling and updates RegExp to match 3 possible cases: <code>url()</code>, <code>url("")</code> and <code>url('')</code>. It also works on jQuery 1 to 3.</p> <pre><code>var getBackgroundImageSize = function(el) { var imageUrl = $(el).css('background-image').match(/^url\(["']?(.+?)["']?\)$/); var dfd = new $.Deferred(); if (imageUrl) { var image = new Image(); image.onload = dfd.resolve; image.onerror = dfd.reject; image.src = imageUrl[1]; } else { dfd.reject(); } return dfd.then(function() { return { width: this.width, height: this.height }; }); }; // // Usage // getBackgroundImageSize(jQuery('#mydiv')) .then(function(size) { console.log('Image size is', size.width, size.height); }) .fail(function() { console.log('Could not get size because could not load image'); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload