Disable Link from HREF Attribute?

J

Jamie Jackson

I know that it's possible to return false in an onClick to disable a
link. Is there a way to do this within the HREF attribute?

Something like:
<a href="javascript: return false;"> ?

Thanks,
Jamie
 
M

McKirahan

Jamie Jackson said:
I know that it's possible to return false in an onClick to disable a
link. Is there a way to do this within the HREF attribute?

Something like:
<a href="javascript: return false;"> ?

Thanks,
Jamie

Perhaps you want this:

javascript:void(0)
 
L

Lasse Reichstein Nielsen

Jamie Jackson said:
I know that it's possible to return false in an onClick to disable a
link. Is there a way to do this within the HREF attribute?

Something like:
<a href="javascript: return false;"> ?

If you decide to use a javascript:-href (which there are good reasons for
not doing <URL:http://jibbering.com/faq/#FAQ4_24>), then to avoid having
the page replaced by the value of the expression, the expression must
evaluate to "undefined".

Example:
<a href="javascript:document.forms[0].elements[1].value='foo';void 0;">...</a>
(The operator "void" always gives the value "undefined", whereas the
variable "undefined" wasn't defined in earlier browsers).

It would still be better to do:
<a href="youNeedJS.html"
onclick="document.forms[0].elements[1].value='foo';return false;">...</a>
or even better:
<input type="button" value="..."
onclick="document.forms[0].elements[1].value='foo';return false;">

/L
 
L

Lee

Jamie Jackson said:
I know that it's possible to return false in an onClick to disable a
link. Is there a way to do this within the HREF attribute?

Something like:
<a href="javascript: return false;"> ?

Why use a link at all?
It might help to know what you're trying to accomplish.
 
J

Jamie Jackson

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top