I want to check the href attribute and if it does not contain the full path, I want to replace it with full URL? Should I use JavaScript which does that but don’t work on IE 8?
My JS:
fullUrl = $(this).filter('[href^='+document.location.protocol+']').attr('href')
? true : false;
url = fullUrl ?
$(this).filter('[href^='+document.location.protocol+']').attr('href')
: document.location.protocol + '//' + document.domain + $(this).attr('href');
I need to check if href contain full url or not:
href: “/path/other.html” (part)
href:”http://domain.com/path/other.html” (full url)
And then if i have part url href, i must add domain and recreate href to full url!
The full url is available via the
.hrefproperty.Typically there’s no need to set it to the attribute, but if you want to, you could do this:
This finds all
<a>elements that have anhrefattribute, and updates itshrefusing theattr()[docs] method by passing a function as the second parameter, which returns the value of the.hrefproperty.If you just wanted a list of them, you can use the
map()[docs] method.Example: http://jsfiddle.net/EDhhD/
EDIT:
If you want to explicitly check that the path isn’t already the full path, just add an
ifstatement to my first example.