/*
Orig via http://jsfiddle.net/csswizardry/yqdFe/
*/
/*
Baseado em http://css-tricks.com/favicons-next-to-external-links/
*/
a { display: block; text-decoration: none; padding: 5px; }
JavaScript
(function(){
function getDomain(url) {
return url.match(/:\/\/(.[^\/\?]+)/)[1];
}
function forEach( collection, fn ){
for (var i = 0, len = collection.length; i < len ; i++){
fn.call( null, collection[i], i)
}
}
function externalFavicon(){
var posts = document.getElementsByTagName('article'),
links = [],
notInThisHost = new RegExp(window.location.hostname),
href;
forEach(posts,function(post){
// Concatenar sem transformar em array(matriz)
Array.prototype.push.apply(links,post.getElementsByTagName('a') )
})
forEach(links, function(link){
href = link.href;
// Vemos que é um link externo (se ele começa com http e não neste domínio), e se isso não é uma imagem
if( /^(http|\/\/)/.test(href) && ! notInThisHost.test(href) && ! /\.(jpe?g|png|gif)/i.test(href) && (link.firstChild && link.firstChild.tagName !== "IMG" )){
// Nós usamos o favicon como plano de fundo e adicionamos um padding para visibilidade
link.style.background = "url(http://" + getDomain(href) + "/favicon.ico) no-repeat 1px center"
link.style.paddingLeft = "18px"
link.style.backgroundSize = "16px"
}
})
}
// Executar a função após a conclusão do carregamento da página para não consumir recursos desnecessários durante o carregamento
window.addEventListener ? window.addEventListener('load',externalFavicon,false): window.attachEvent('onload',externalFavicon)
})()
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.