views:

66

answers:

4

I am using a javascript editor called NicEdit which doesn't come with an option to change or reference the base url. I was wondering if this can be done in jquery or javascript?

The image src of all images in the DIV element are the same, <IMG border=0 src="/image.jpg"> except the actually images is stored under a different domain and directory altogether.

I like the src to remain /image.jpg, but make reference to all images as with the domain http://www.domain.com/image.jpg within the that DIV only.

Possible?

A: 

I guess this would work:

   $('#divid img').each(function() {
       var newsrc = 'http://www.domain.com' + $(this).attr('src');
       $(this).attr('src', newsrc)
    })
Per Holmäng
Better put `$(this)` in a variable.
Gumbo
A: 

Not too difficult with jQuery, but the kind of thing you should consider doing server-side so that your links don't all break when JS is turned off.

$("#div img").each(function(i, e) {
  $(e).attr("src", "http://www.domain.com/" + $(e).attr("src"));
});
meagar
A: 

this allows external images to exist and not be affected (not a very deep check though)

$( 'img' ).not( '[src*="://"]' ).each( function( )
{
    $( this ).attr( 'src', 'http://whatevs.com' + $( this ).attr( 'src' ) ); }
} );
Dan Beam
A: 

These are all great answers and work, except my problem looks deeper than I wish for, when a http:// is missing from the src this NicEdit script places the URL of the web site in return.

This with your answers displays 2 hrefs within the src. Looks like my only solutions at this point is add and remove domains through PHP or find another editor the behaves nicely.

Thanks Again.

Tim
Fine, but if your *original* question has been answered you can still mark the most helpful one as "accepted" (click the big checkmark icon to the left of the question).
Crescent Fresh