I wrote some days ago a simple thing to fallback to an placeholder image if the desired image can't loaded. It allows the user to use 2 placeholders to to define the image size:
That's the thing I wrote:
;(window.jQuery || window.Zepto).fn.fallback = function (url) {
return this.one('error', function () {
this.src = (url|| 'http://lorempixel.com/$w/$h')
.replace('$w', this.width).replace('$h', this.height);
});
};
I'm asking me now, if it is possible to replace .replace('$w', this.width).replace('$h', this.height);
with an shorter but equal regex
to replace all $*
(dollar + first-char) with an assigned value from any object?
Something like this:
'$f is not equal to $b'.replace(/magicregex/, {
foo: 'foo',
bar: 'bar'
});
So that we can use all properties
from the image-object
, e.g. image.width
, image.src
, image.width
...