Abrir sempre o link externo em nova janela e marcar a tag com CSS

8 de dezembro, 2006 por Ricardo José Corrêa
1 Star2 Stars3 Stars4 Stars5 Stars (1 voto, média: 5 de 5)
Loading ... Loading ...

O script visto neste artigo tem a função de que sempre que um link tiver a referência como “external” abra em nova janela, e os que tiverem o target=”_blank” sejam setados como rel=”external”, além de mudar/incluir o nome da class no link, para que assim todos tenham um marcardor (iconografia de link externo) de link externo.
O legal é que não precisa fazer mais nada, o .js incluso na página se vira.

external.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Troca CSS e abre em nova janela, sempre</title>
<style>
.linkExterno { background:url("external.gif") right 2px no-repeat; cursor:pointer; padding-right:13px; }
</style>
</head>
<body>
<script xsrc="external.js" mce_src="external.js"     ></script>
<a xhref="http://www.rjcorrea.com.br/#" mce_href="http://www.rjcorrea.com.br/#"     target="_blank">Link
externo com target blank</a><br>
<a xhref="http://www.rjcorrea.com.br/#" mce_href="http://www.rjcorrea.com.br/#"     rel="external">Link
externo com rel external</a>
</body>
</html>

external.js

///////////////////////////////////////////////////////////////////////////
////////////////////////////////// RjCorrêa ///////////////////////////////
/////Abrir sempre o link externo em nova janela e marcar a tag com CSS/////
///////////////////////www.rjcorrea.com.br/////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//////////////////////V. 2.0 /// Ajustada Por Fábio A. ////////////////////
///////////////////////////fabioarantes@gmail.com//////////////////////////
var Trocar = new Object();
Trocar.verify = function () {
var nav = navigator.appName;
if(nav != "Microsoft Internet Explorer"){
document.addEventListener("DOMContentLoaded", Trocar.trocar, false);
} else {
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch( error ) {
setTimeout( Trocar.trocar, 0 );
return;
}
}
}
Trocar.trocar = function () {
if (document.getElementsByTagName) {
links = document.getElementsByTagName('a');
for (k = 0; k < links.length; k++) {
if (links[k].getAttribute('href') &amp;&amp; links[k].getAttribute('target') == "_blank" || links[k].getAttribute('rel') == "external"){
links[k].className = "linkExterno";
links[k].target = '_blank';
links[k].rel = 'external';
}
}
}
}
 
Trocar.verify();

Technorati Tags: , ,

Compartilhe e aproveite:
  • del.icio.us
  • Technorati
  • Digg
  • Facebook
  • Google
  • Reddit
  • Furl
  • Mixx
  • Live
  • E-mail this story to a friend!

1 comentário, já começou :) to “Abrir sempre o link externo em nova janela e marcar a tag com CSS”

  1. Lurdes CostaNo Gravatar Says:

    Parabéns. Muito interessante e digno de estudo.

Deixe um comentário


I love beerI love CMSI love DesignI love DreamweaverI love FlashI love FoodI love GamesI love MoneyI love MusicI love PhotoshopI love PHPI love ProgrammingI love ShoppingI love WindowsI love Wine
Site protegido Feito com a cabeça PageRank Add to Technorati Favorites