Jamie Jackson said:
Why use a link at all?
It might help to know what you're trying to accomplish.
Okay, I'm tasked to put disclaimers on external links. However, the
site is data-driven, and I'd like to come up with a way to do this in
the least obtrusive way (without going around and changing hardcoded
links, and links within databased "articles.")
Here's what I came up with, but it's working in Opera, and not in IE.
(Any insight into getting it to work with IE?)
Overview: I'm using JS to retroactively give every external link an
onClick prompt method. I'm a newbie, so I'd appreciate any peripheral
tips, too.
<script type="text/javascript">
function promptBeforeOpening () {
if (confirm("LEAVING SITE X\n\nYou are about to leave site X \n\nTo
proceed to the Web site, please select the OK button.")) {
// confirmed: thank you, drive through
return true;
} else {
// user has cancelled, don't go to link
return false;
}
}
/* The following function will take every external link on the page,
** and modify it to use a prompt
*/
function disclaimOutsideLinks(d, urlExclusions) {
var href="";
var exclusionPatterns = new Array();
var matchFound;
var origOnClick;
// make an array of exclusion patterns for later use
for (var h=0; h < urlExclusions.length; h++) {
exclusionPatterns[h] = new RegExp(urlExclusions[h], 'i');
}
// loop over all links
for (var i=0; i < d.links.length; i++) {
// reset some vars to their defaults
matchFound = false;
origOnClick = "";
// loop over all exclusion patterns
for (var j=0; j < exclusionPatterns.length; j++) {
if (exclusionPatterns[j].test( d.links
.href ) ) {
// exclusion pattern matched, set matchFound to true, and
break loop
matchFound = true;
break;
}
}
// if matchFound is false, tweak the link's attributes
if (! matchFound ) {
// save a copy of any onClick that the link may have originally
had
origOnClick = d.links.onclick;
// rewrite the link's onClick to prompt before going
d.links.onclick = "return promptBeforeOpening();";
// d.links.href += "#hi"; // debugging
if (origOnClick != null) {
// old onClick exists, so append it to the new one
d.links.onclick += origOnClick;
}
}
}
}
/* URL Exclusion Array
** URLs that have this substring should not be disclaimed
*/
urlExclusions = new Array(
"subdomain.stage.domain.com",
"subdomain.dev.domain.com",
"dom1.com/dir1/dir2",
"dom2.com/dir3/dir4",
".gov",
"mailto:",
"excludeMeFromDisclaimer"
);
// Switch all of the document's links now
disclaimOutsideLinks(document, urlExclusions);
</script>