PDA

View Full Version : url titles



phipm1
08-19-2011, 02:13 PM
I have javascript code puting url title on external links but it is also putting them on my internal and email links as well.

I can't figure out how to not have url title on internal and email links.

I've attached my code.

extlinkTitle.js

function extlinkTitle() {
var mat=[];
var anc=document.getElementsByTagName('a');
for(c=0;c<anc.length;c++){
mat[c]=anc[c].href.match(/quicklinks*/g);
if(mat[c]==null){
anc[c].title='you are leaving';
}
}
}
window.addEventListener?
window.addEventListener('load',extlinkTitle,false):
window.attachEvent('onload',extlinkTitle);

jscheuer1
08-19-2011, 10:05 PM
First off, in most browsers this sort of thing doesn't really work well all of the time locally. You would have to test it on a live page.

And that code assumes your domain name includes the word quicklinks, does it?

Anyway, there are various ways to deal with it. The easiest might be to put your domain name in there and mailto, like instead of:


mat[c]=anc[c].href.match(/quicklinks*/g);

Do like:


mat[c]=anc[c].href.match(/(mydomainname)|(mailto:)/);

where mydomainname is your actual domain name. But there are a number of things I don't like about that code.

And we can get the domain, actually the hostname from javascript, and you can add in mailto: for mail links. What I'd do is scrap that script and use instead:


<script type="text/javascript">
(function(){
var linktitle = "You're Leaving!", // Configure Title for External Links
h = location.hostname.replace(/www\./, ''),
re = new RegExp('(' + h + ')|(mailto:)');
function externalLink(){
var a = document.links;
for(var i = 0; i < a.length; ++i){
if(!re.test(a[i].href)){
a[i].title = linktitle;
}
}
}
if (window.addEventListener){
window.addEventListener('load', externalLink, false);
}
else if (window.attachEvent){
window.attachEvent('onload', externalLink);
}
})();
</script>

Demo:

http://home.comcast.net/~jscheuer1/side/external_links.htm