Reputation: 409
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
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
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
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
Reputation: 2141
Here you are:
$('a').each(function () {
$(this).prop('href', $(this).prop('href')
.replace(/^http:\/\/stackoverflow\.com/, ''));
});
Hope this help.
Upvotes: 4
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