JavaScript
Article

Update images with a specific src (relative path) with absolute path

By Sam Deering

jQuery code snippet to update images with a domain name, turning it from a relative path into an absolute path. Sometimes useful when testing across domains with the same static content or pulling images from an external domain.

replace-url-absolutw

(function ($) {
    $(document).ready(function () {
        $('img').each(function (i, v) {
            var $el = $(this),
                s = $el.attr('src'),
                sRx = /[/res/images/.*]+/igm;
            console.log(s);
console.log(s.test(sRx));
            if (s.test(sRx)) {
                console.log('match');
                s = 'http://splash.abc.net.au' + s;
                $el.attr('src', s);
            }
        });
    });
})(jQuery);

jsfiddle.net/BK9Zp/

JSfiddly lolz.

There is an awesome project by Ben Alman which has all the bells and whistles in this area check it out:
https://raw.github.com/cowboy/jquery-urlinternal/master/jquery.ba-urlinternal.js

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.