PDA

View Full Version : How to get the title of an external page with jQuery



Snookerman
04-11-2009, 07:37 AM
I found a nice script that uses jQuery to insert the favicon of an external page after any external link. My code looks like this:

$('a[href^="http://"]').filter(function(){
return this.hostname && this.hostname !== location.hostname;
}).each(function(){
var faviconURL = $(this).attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
var faviconIMG = $('<img class="favicon">').insertAfter(this);
var extImg = new Image();
extImg.src = faviconURL;
if (extImg.complete)
faviconIMG.attr('src', faviconURL);
else
extImg.onload = function(){
faviconIMG.attr('src', faviconURL);
};
});
The original script was found here: http://blog.liviuholhos.com/javascript/add-a-favicon-near-external-links-with-jquery (http://blog.liviuholhos.com/javascript/add-a-favicon-near-external-links-with-jquery)

What I am trying to do now is to get the title of the external page and use it as the alt attribute for each favicon image. Can someone please help me?
Also, can someone explain what the if else statement does?

Thanks!