[thelist] Handling external links with JS

Brian Cummiskey Brian at hondaswap.com
Thu Feb 5 10:10:21 CST 2004

I created/found/messed with/tweaked a simple function to handle external
links without using a <a "rel=external" href..."> or a target attribute
to keep a xhtml strict DTD.

It works great for notmydomain.com sites.

function handleExternalLinks(){
	var anchors = document.getElementsByTagName("a");
	var i, href;
	for(i=0; i < anchors.length; i++){
		if(!anchors[i].href) continue;
		href = anchors[i].href;
		if(href.indexOf("mydomain.com") == -1){ // Href is not a
file on my server
			if(href.indexOf("javascript:") == -1){ // Href
is not a javascript call
				if(!anchors[i].onclick){ // Href does
not have an onclick event
					if(href.indexOf("mailto:") ==
-1){ // Href is not a mailto:
if(href.indexOf("http://") != -1){ // Href is not relative (for Safari)


Now, I want to add one more attribute to this:  If the link is to a file
called "mydomain.com/out.php" to open in a new window (as out.php, is a
counter++ then a header re-direct to a passed query string url to

I tried adding: 

if(href.indexOf("out.php") == 1){ // Href is on an out page

To no avail.

Any suggestions?

More information about the thelist mailing list