[thelist] Handling external links with JS

Toby Mills toby at tobymills.com
Thu Feb 5 10:19:25 CST 2004


Firstly, the target attribute for links is being depreciated by the W3C. The
best method I have found to get around this is to use javascript to open the
link in a new window.

Otherwise, to answer your question, try :
if(window.location.href.toLowerCase().indexOf("out.php") >= 0){/*is out
page*/}


T
-------------------------------
toby mills
web developer & programmer


www.tobymills.com

 

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Brian Cummiskey
Sent: 05 February 2004 16:10
To: thelist at lists.evolt.org
Subject: [thelist] Handling external links with JS

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)
	
anchors[i].setAttribute("target","_blank");
						}
					}
				}
			}
		}

	}
}


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
someotherdomain.com)

I tried adding: 

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

To no avail.

Any suggestions?


-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 




More information about the thelist mailing list