How to detect when a link is clicked

Discussion in 'Javascript' started by Mat, Aug 26, 2003.

  1. Mat

    Mat Guest

    How can I detect when a link has been clicked but the new page is
    still in the process of loading? The document.location.href property
    still displays the current location (understandably) not the one
    that's about to load.

    I have a page that reloads every 30 seconds in order to access live
    data. If a user clicks on a link just prior to the page reloading the
    reload takes precedence over the link click and this is annoying for
    the users. Whe the page is about to reload I want to check to see if a
    link has just been clicked (and therefore a new page is about to load)
    and, if so, cancel the reload

    Any suggestions as to how I can detect this (without having to put and
    onclick in every link) would be appreciated
     
    Mat, Aug 26, 2003
    #1
    1. Advertising

  2. Mat

    Grant Wagner Guest

    Mat wrote:

    > How can I detect when a link has been clicked but the new page is
    > still in the process of loading? The document.location.href property
    > still displays the current location (understandably) not the one
    > that's about to load.
    >
    > I have a page that reloads every 30 seconds in order to access live
    > data. If a user clicks on a link just prior to the page reloading the
    > reload takes precedence over the link click and this is annoying for
    > the users. Whe the page is about to reload I want to check to see if a
    > link has just been clicked (and therefore a new page is about to load)
    > and, if so, cancel the reload
    >
    > Any suggestions as to how I can detect this (without having to put and
    > onclick in every link) would be appreciated


    You have to put it on each an every link, but if the links don't currently
    have onclick events, it isn't that big a deal:

    <body onload="window.reloadTimer =
    setTimeout('window.location.reload(true);', 30000);setOnClick(onClick);">
    <script type="text/javascript">
    function onClick() {
    if (window.reloadTimer != null) {
    clearTimeout(window.reloadTimer);
    }
    return true;
    }
    function setOnClick(f) {
    // to minimize page size, rather than defining separate onmouseout events
    for
    // every link, any link without a locally defined onmouseout event is set
    here

    function __setOnClick(dl, f) {
    if (dl) {
    for (var i = 0; i < dl.length; i++) {
    if (dl.onclick == null) {
    dl.onclick = f;
    }
    }
    }
    } // __setOnClick()


    __setOnClick(document.links, f);

    if (document.layers) {
    var dl = document.layers;
    for (var i = 0; i < dl.length; i++) {
    if (dl && dl.document) {
    __setOnClick(dl.document.links, f);
    }
    }
    }
    } // setOnClick()
    </script>
    </body>

    The whole document.layers bit is to support Netscape 4. I just realized it
    should be a recursive call, since you can nest layers within layers, but I
    never do that so I didn't write it that way. If you don't need to support
    Netscape 4, or don't ever use layers in Netscape 4, you could remove that
    bit.

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Aug 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2ZQ==?=

    Detect which button was clicked?

    =?Utf-8?B?RGF2ZQ==?=, Feb 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,448
    =?Utf-8?B?U3VyZXNo?=
    Feb 26, 2004
  2. who be dat?
    Replies:
    7
    Views:
    388
    who be dat?
    Jul 13, 2004
  3. Ben Amada
    Replies:
    1
    Views:
    424
    Ben Amada
    Jan 3, 2005
  4. Replies:
    9
    Views:
    21,908
  5. Brad Wood
    Replies:
    2
    Views:
    347
    Bruce Barker
    Jul 18, 2005
Loading...

Share This Page