Results 1 to 2 of 2

Thread: url titles

  1. #1
    Join Date
    Jul 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default url titles

    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);

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

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

    Code:
    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:

    Code:
    <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/s...rnal_links.htm
    Last edited by jscheuer1; 08-20-2011 at 05:23 AM. Reason: code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •