Stopping URL from loading (from anchor)

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

    #1

    Stopping URL from loading (from anchor)

    Hi all,

    I realy need your help.

    I have a page with different web links, now what I am trying to do is
    whenever a user clicks on a link that link is captured using
    event.target method. I'm using this to process that URL, but the real
    part is stopping the document from loading that URL. I tried stop()
    method but its not working. I also tried asigning current URL of the
    document to the document's location but it failed to work.

    Is there any workaround to stop the document from loading a URL.

    I don't want to change the anchors. I just want to use that workaround
    or method after calling event.target() method.

    Thanks.
    Any help is appreciated.
  • Guido Wesdorp

    #2
    Re: Stopping URL from loading (from anchor)

    saif wrote:[color=blue]
    > Hi all,
    >
    > I realy need your help.
    >
    > I have a page with different web links, now what I am trying to do is
    > whenever a user clicks on a link that link is captured using
    > event.target method. I'm using this to process that URL, but the real
    > part is stopping the document from loading that URL. I tried stop()
    > method but its not working. I also tried asigning current URL of the
    > document to the document's location but it failed to work.
    >
    > Is there any workaround to stop the document from loading a URL.
    >
    > I don't want to change the anchors. I just want to use that workaround
    > or method after calling event.target() method.
    >
    > Thanks.
    > Any help is appreciated.[/color]

    I guess what you want to do is use JavaScript to find all anchors in a
    document (document.getEl ementsByTagName ('a')) and put an onclick on
    them, that does what you want it to do and also cancels the event
    ('event.prevent Default()' on Mozilla, 'event.returnVa lue = false' on IE).

    Small example:

    =============== =============== =============== =============== ===========

    function process_link_cl ick(event) {
    var link = event.target ? event.target : event.srcElemen t;
    var href = link.getAttribu te('href');
    // do something with your href here
    if (event.preventD efault) {
    // mozilla
    event.preventDe fault();
    } else {
    event.returnVal ue = false;
    };
    };

    function find_and_patch_ links() {
    var links = document.getEle mentsByTagName( 'a');
    for (var i=0; i < links; i++) {
    if (link.addEventL istener) {
    // mozilla
    link.addEventLi stener('click', process_link_cl ick, false);
    } else if (link.attachEve nt) {
    // IE
    link.attachEven t('onclick', process_link_cl ick);
    };
    };
    };

    =============== =============== =============== =============== ===========

    You will have to make sure the find_and_patch_ links() function is called
    on load of the document (<body onload="find_an d_patch_links() ">). Note
    that I haven't tested this code so it may contain a bug or two ;)

    Cheers,

    Guido

    Comment

    • Michael Winter

      #3
      Re: Stopping URL from loading (from anchor)

      On Tue, 04 May 2004 11:37:52 +0200, Guido Wesdorp <guido@infrae.c om> wrote:

      [snip]
      [color=blue]
      > I guess what you want to do is use JavaScript to find all anchors in a
      > document (document.getEl ementsByTagName ('a'))[/color]

      A more reliable approach is to use the document.links collection. It's
      supported by more browsers.
      [color=blue]
      > and put an onclick on them, that does what you want it to do and also
      > cancels the event ('event.prevent Default()' on Mozilla,
      > 'event.returnVa lue = false' on IE).[/color]

      If you use other approaches for attaching listeners (which I've added
      below), returning false will suffice.

      As we're attaching listeners directly to the A elements, one can simply
      use the this operator to get a reference to the element.
      [color=blue]
      > function process_link_cl ick(event) {
      > var link = event.target ? event.target : event.srcElemen t;
      > var href = link.getAttribu te('href');[/color]

      Unless you're working with XML, link.href will do and is more reliable.
      [color=blue]
      > // do something with your href here
      > if (event.preventD efault) {
      > // mozilla
      > event.preventDe fault();
      > } else {
      > event.returnVal ue = false;
      > };
      > };[/color]

      By the way, you don't usually need to terminate blocks with a semi-colon.
      The only exceptions I can think of at the moment are with the function
      operator

      ref = function() {
      };

      and the object literal

      obj = {};

      Notice that the difference is due to the assignment which, as a statement,
      should be terminated.
      [color=blue]
      > function find_and_patch_ links() {
      > var links = document.getEle mentsByTagName( 'a');[/color]

      var links = documents.links ;

      is more compatible.
      [color=blue]
      > for (var i=0; i < links; i++) {[/color]

      That's an error. In any case, it's better to save the property value
      rather than repeatedly looking it up:

      for( var i = 0, n = links.length; i < n; ++i ) {
      [color=blue]
      > if (link.addEventL istener) {[/color]

      This is also an error as 'link' is undefined.
      [color=blue]
      > // mozilla
      > link.addEventLi stener('click', process_link_cl ick, false);
      > } else if (link.attachEve nt) {[/color]

      Unfortunately, Microsoft's attachEvent() mechanism is flawed and doesn't
      set the this operator correctly. As I'm adding support for older browsers
      anyway, we'll skip attachEvent() and use the event properties with IE.
      [color=blue]
      > // IE
      > link.attachEven t('onclick', process_link_cl ick);
      > };
      > };
      > };[/color]

      The culmination of which produces:

      function process_link_cl ick( event ) {
      var href = this.href;
      event = event || window.event;

      // do something with your href here
      // ...

      if( event.preventDe fault ) {
      event.preventDe fault();
      }
      return false;
      }

      function find_and_patch_ links() {
      var links = document.links, link;

      for( var i = 0, n = links.length; i < n; ++i ) {
      link = links[ i ];

      if( link.addEventLi stener ) {
      link.addEventLi stener( 'click', process_link_cl ick, false );
      } else {
      link.onclick = process_link_cl ick;
      }
      }
      }

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Shawn Milo

        #4
        Re: Stopping URL from loading (from anchor)

        I think that this is the easiest way to do it:

        <a href="http://www.google.com" >link1</a> will load<br/>
        <a href="http://www.google.com" onclick="return false;">link2</a> will not load<br/>

        Shawn

        Comment

        • Guido Wesdorp

          #5
          Re: Stopping URL from loading (from anchor)

          Michael Winter wrote:[color=blue]
          >
          > The culmination of which produces:
          >
          > function process_link_cl ick( event ) {
          > var href = this.href;
          > event = event || window.event;
          >
          > // do something with your href here
          > // ...
          >
          > if( event.preventDe fault ) {
          > event.preventDe fault();
          > }
          > return false;
          > }
          >[/color]
          Are you sure 'return false;' is sufficient? I remember trying it but
          with no avail, could be I was doing something wrong as well... I do know
          it works when you define an event handler on an element (<a
          onclick="return false;">) but not when using JavaScript to dynamically
          attach them...
          Anyway, thanks for fixing the bugs, and thanks for making it less
          browser-specific. I write Mozilla/IE apps usually (check out Kupu,
          kupu.oscom.org, for example), without having to care about older/other
          browsers, so I don't know much about writing 'legacy' code...

          Cheers,

          Guido

          Comment

          • Michael Winter

            #6
            Re: Stopping URL from loading (from anchor)

            On Tue, 04 May 2004 15:42:55 +0200, Guido Wesdorp <guido@infrae.c om> wrote:
            [color=blue]
            > Michael Winter wrote:[color=green]
            >> The culmination of which produces:
            >>
            >> function process_link_cl ick( event ) {
            >> var href = this.href;
            >> event = event || window.event;
            >>
            >> // do something with your href here
            >> // ...
            >>
            >> if( event.preventDe fault ) {
            >> event.preventDe fault();
            >> }
            >> return false;
            >> }[/color]
            >
            > Are you sure 'return false;' is sufficient? I remember trying it but
            > with no avail, could be I was doing something wrong as well... I do know
            > it works when you define an event handler on an element (<a
            > onclick="return false;">) but not when using JavaScript to dynamically
            > attach them...[/color]

            Probably not with listeners added with addEventListene r(), which is why I
            left the preventDefault( ) call. Returning false isn't specified in DOM 2
            Events, so it's probably not implemented and why should it? The legacy
            argument doesn't apply there. However, returning false is sufficient for
            the event properties as it is effectively the same as specifying the
            intrinsic event in HTML. Using the returnValue property doesn't appear to
            be necessary in IE with attachEvent() but it wouldn't hurt (I usually
            avoid that mechanism like the plague, though).
            [color=blue]
            > Anyway, thanks for fixing the bugs, and thanks for making it less
            > browser-specific. I write Mozilla/IE apps usually (check out Kupu,
            > kupu.oscom.org, for example), without having to care about older/other
            > browsers, so I don't know much about writing 'legacy' code...[/color]

            You're welcome.

            Mike

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • saif

              #7
              Re: Stopping URL from loading (from anchor)

              Thanks Wesdorp, your code snippet really helped me.

              This is what I was looking for:

              if (event.preventD efault) {
              // mozilla
              event.preventDe fault();
              } else {
              event.returnVal ue = false;
              };

              Surprisingly I didn't find any 'preventDefault ' discription in
              JavaScript documentation from Netscape.

              Thanks again.

              Comment

              • Michael Winter

                #8
                Re: Stopping URL from loading (from anchor)

                On 4 May 2004 08:40:33 -0700, saif <unmssa@yahoo.c om> wrote:
                [color=blue]
                > Thanks Wesdorp, your code snippet really helped me.
                >
                > This is what I was looking for:
                >
                > if (event.preventD efault) {
                > // mozilla
                > event.preventDe fault();
                > } else {
                > event.returnVal ue = false;
                > };
                >
                > Surprisingly I didn't find any 'preventDefault ' discription in
                > JavaScript documentation from Netscape.[/color]

                That's because it's part of the World Wide Web Consortium's (W3C) Document
                Object Model (DOM). Specifically, DOM Level 2 Events. The DOM home is here:

                <URL:http://www.w3.org/DOM/>

                The Technical Reports section contains the various specifications. It's
                best to avoid Level 3 for now as most browsers don't implement it.

                <URL:http://www.w3.org/DOM/DOMTR/>

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • Guido Wesdorp

                  #9
                  Re: Stopping URL from loading (from anchor)

                  Michael Winter wrote:[color=blue]
                  >
                  > <URL:http://www.w3.org/DOM/>
                  >
                  > <URL:http://www.w3.org/DOM/DOMTR/>
                  >[/color]
                  Mozilla's DOM documentation is pretty useful here as well:



                  Cheers,

                  Guido

                  Comment

                  Working...