user472285
user472285

Reputation: 2674

Remove part of image src attribute

<img width="49" alt="[alternative text]" src="http://win-23ookphjfn0:80/sites/my/User%20Photos/Images%20du%20profil/WIN-23OOK_Administrateur_MThumb.jpg">

<img width="49" alt="[alternative text]" src="/sites/my/User%20Photos/Images%20du%20profil/WIN-23OOK_Administrateur_MThumb.jpg">

What is the best way to remove http://win-23ookphjfn0:80 in the src? Javascript or JQuery?

I have different environment http://win-23ookphjfn0:80 can change...

Upvotes: 2

Views: 3252

Answers (3)

maxedison
maxedison

Reputation: 17553

I think the easiest way to do this is to split the src string into an array, change the part of it that you want (by modifying that part's corresponding item in the array), and then join the array back together as a string:

$('img').each(function() {
    var src = this.src; //get img src
    var arr = src.split('/'); //produces an array. the part you want to change is the 3rd element ([2])
    arr[2] = 'whatever I want';
    var newSrc = arr.join('/');
    this.src = newSrc;
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

$("IMG").each(function() {
    $(this).attr("src", $(this).attr("src").replace("http://win-23ookphjfn0:80", ""));
});

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try this:

$(document).ready(function() {
       $("img").each(function() {
          $(this).attr("src", $(this).attr("src").replace("http://win-23ookphjfn0:80", ""));
       });
    });

Hope it helps

Upvotes: 1

Related Questions