Disable Link from HREF Attribute?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Jackson

    Disable Link from HREF Attribute?

    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="javascrip t: return false;"> ?

    Thanks,
    Jamie
  • McKirahan

    #2
    Re: Disable Link from HREF Attribute?

    "Jamie Jackson" <mcsqueebagePle aseDontSpamMe@h otmail.com> wrote in message
    news:bkcqn0d2ar cc143l12csijpfm 7cgedtn5d@4ax.c om...[color=blue]
    > 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="javascrip t: return false;"> ?
    >
    > Thanks,
    > Jamie[/color]

    Perhaps you want this:

    javascript:void (0)


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Disable Link from HREF Attribute?

      Jamie Jackson <mcsqueebagePle aseDontSpamMe@h otmail.com> writes:
      [color=blue]
      > 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="javascrip t: return false;"> ?[/color]

      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="javascrip t:document.form s[0].elements[1].value='foo';vo id 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="docume nt.forms[0].elements[1].value='foo';re turn false;">...</a>
      or even better:
      <input type="button" value="..."
      onclick="docume nt.forms[0].elements[1].value='foo';re turn false;">

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Lee

        #4
        Re: Disable Link from HREF Attribute?

        Jamie Jackson said:[color=blue]
        >
        >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="javascrip t: return false;"> ?[/color]

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

        Comment

        • Jamie Jackson

          #5
          Re: Disable Link from HREF Attribute?

          On 25 Oct 2004 10:36:59 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:
          [color=blue]
          >Jamie Jackson said:[color=green]
          >>
          >>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="javascrip t: return false;"> ?[/color]
          >
          >Why use a link at all?
          >It might help to know what you're trying to accomplish.[/color]

          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 promptBeforeOpe ning () {
          if (confirm("LEAVI NG 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 disclaimOutside Links(d, urlExclusions) {
          var href="";
          var exclusionPatter ns = new Array();
          var matchFound;
          var origOnClick;
          // make an array of exclusion patterns for later use
          for (var h=0; h < urlExclusions.l ength; h++) {
          exclusionPatter ns[h] = new RegExp(urlExclu sions[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 < exclusionPatter ns.length; j++) {
          if (exclusionPatte rns[j].test( d.links[i].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[i].onclick;
          // rewrite the link's onClick to prompt before going
          d.links[i].onclick = "return promptBeforeOpe ning();";
          // d.links[i].href += "#hi"; // debugging
          if (origOnClick != null) {
          // old onClick exists, so append it to the new one
          d.links[i].onclick += origOnClick;
          }
          }
          }
          }
          /* URL Exclusion Array
          ** URLs that have this substring should not be disclaimed
          */
          urlExclusions = new Array(
          "subdomain.stag e.domain.com",
          "subdomain.dev. domain.com",
          "dom1.com/dir1/dir2",
          "dom2.com/dir3/dir4",
          ".gov",
          "mailto:",
          "excludeMeFromD isclaimer"
          );
          // Switch all of the document's links now
          disclaimOutside Links(document, urlExclusions);
          </script>

          Comment

          Working...