javascript - How do I check href attribute and if needed add full url address? -


i want check href attribute , if not contain full path, want replace full url? should use javascript 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 check if href contain full url or not:

href: "/path/other.html" (part)

href:"http://domain.com/path/other.html" (full url)

and if have part url href, must add domain , recreate href full url!

the full url available via .href property.

typically there's no need set attribute, if want to, this:

$('a[href]').attr('href', function() { return this.href; }); 

this finds <a> elements have href attribute, , updates href using attr()[docs] method passing function second parameter, returns value of .href property.


if wanted list of them, can use map()[docs] method.

var hrefs = $('a[href]').map(function() { return this.href; }).get(); 

example: http://jsfiddle.net/edhhd/


edit:

if want explicitly check path isn't full path, add if statement first example.

$('a[href]').attr('href', function(i,hrf) {      if( hrf.indexof( 'http' ) !== 0 ) return this.href;  }); 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -