Jim
Jim

Reputation: 1440

Remove part of link from each href

I need to remove from each link in the page part of the link

The current link like this

http://domain.com/download.php?url=https://www.dropbox.com/file1.rar

I need the link to be like this

https://www.dropbox.com/file1.rar

So just remove this http://domain.com/download.php?url=

This Is my code

<a href="http://domain.com/download.php?url=https://www.dropbox.com/file1.rar">Download File 1</a><br>
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file2.rar">Download File 2</a><br>
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file3.rar">Download File 3</a><br>
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file4.rar">Download File 4</a><br>
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file5.rar">Download File 5</a><br>
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file6.rar">Download File 6</a><br>

<a href="https://www.dropbox.com/file9.rar">Download dropbox 1</a><br>
<a href="https://www.dropbox.com/file8.rar">Download dropbox 2</a><br>

<a href="https://www.google.com">Google</a><br>
<a href="https://domain.com">HomePage</a><br>

I managed to select the links that need to be replaced by jQuery

$("a[href*='download.php?url=']")

But I need help to remove this part only http://domain.com/download.php?url=

code in jsfiddle

http://jsfiddle.net/Jim_Toth/dB6nW/

the result I need like this

http://jsfiddle.net/Jim_Toth/dB6nW/1/

Upvotes: 0

Views: 1349

Answers (5)

Teemu
Teemu

Reputation: 23396

Use split():

$('a[href*="download.php?url="]').attr('href', function () {
    return $(this).attr('href').split('=')[1];
});

Notice the implicit iteration, each() is not needed.

A live demo at jsFiddle.

Upvotes: 1

Paulo Lima
Paulo Lima

Reputation: 1238

I think what you need is this:

var links = $("a[href*='download.php?url=']");

for(var i = 0; i < links.length; i++){
var current = links.eq(i);    
      var href = current.attr("href");
   var newHref = href.substr(href.indexOf("="), href.length);    
   current.attr("href", newHref);
}

http://jsfiddle.net/dB6nW/7/

Upvotes: 0

rockStar
rockStar

Reputation: 1296

var stringToRemove = 'http://domain.com/download.php?url=';
$('a').each(function(){
    var link = $(this).attr('href');
    var newLink = link.replace(stringToRemove, '');
    console.log(newLink);
});

A little simplified script.

Upvotes: 0

melvas
melvas

Reputation: 2356

It's fix your problem:

$("a[href*='download.php?url=']").each(function(){
    var t = $(this);
    var url = t.attr('href').replace('http://domain.com/download.php?url=', '');
    t.attr('href', url);
})

http://jsfiddle.net/dB6nW/5/

Upvotes: 1

renanleandrof
renanleandrof

Reputation: 6997

Edit:

$(function () {
$("a[href*='download.php?url=']").each(function(i,v){
var oldUrl = $(this).attr('href');
var newUrl = oldUrl.replace("http://domain.com/download.php?url=","");
$(this).attr('href', newUrl);
});
});

Upvotes: 0

Related Questions