Danin Na
Danin Na

Reputation: 409

remove some part of a link's href value

I have some link with href attribute.

<a href="http://stackoverflow.com/...variable1.../">My Link</a>
<a href="http://stackoverflow.com/...variable2.../">My Link</a>
<a href="http://stackoverflow.com/...variable3.../">My Link</a>

I need to get the href value , delete http://stackoverflow.com and convert link to

<a href="/...variable1.../">My Link</a>
<a href="/...variable2.../">My Link</a>
<a href="/...variable3.../">My Link</a>

For example

$('a').each(function () {  

     var full_link = $('a').attr('href') ;
     var delete_part = 'http://stackoverflow.com' ;
     var output = full_link - delete_part ;

     $('a').attr('href',output);

});

So what would you suggest me ?

Upvotes: 1

Views: 174

Answers (5)

Sarath Kumar
Sarath Kumar

Reputation: 2353

$('a').each(function () {  
     var full_link = $('a').attr('href') ;
     var delete_part = 'http://stackoverflow.com' ;
     var output = full_link.replace(delete_part, "") ;
     $('a').attr('href',output);
});

Upvotes: 1

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

Try this one..

$('a').each(function() {  
     var full_link = $('a').attr('href');
     var delete_part = 'http://stackoverflow.com';
     var output = full_link.replace(delete_part,""); 
     $('a').attr('href',output);
});

Upvotes: 2

PHP Worm...
PHP Worm...

Reputation: 4224

Try this:

It will remove your domain name irrespective of what it is.

It will work for all domain names no need to specify the domain name

$('a').each(function () {  

     var full_link = $('a').attr('href') ;
     var output =  full_link.replace(/http?:\/\/[^\/]+/i, "");
     $('a').attr('href',output);

});

Upvotes: 2

hungndv
hungndv

Reputation: 2141

Here you are:

$('a').each(function () {
    $(this).prop('href', $(this).prop('href')
        .replace(/^http:\/\/stackoverflow\.com/, ''));
});

Hope this help.

Upvotes: 4

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28445

Please update the code to following

 var full_link = $('a').attr('href') ;
 var delete_part = 'http://stackoverflow.com' ;
 var output = full_link.replace(delete_part, "") ;

For reference - http://www.w3schools.com/jsref/jsref_replace.asp

Upvotes: 3

Related Questions