onunload action

Discussion in 'Javascript' started by Frank, Jul 19, 2004.

  1. Frank

    Frank Guest

    Hi,
    I have an onunload event in the body tag. When it is activated I want to
    know if a <a></a> href was clicked and which one. Is there some document
    property or any other object available for such a thing?

    No, thanks, I don't want to use the onclick or any other event on the <a>
    tag.

    Thanks

    Frank
     
    Frank, Jul 19, 2004
    #1
    1. Advertisements

  2. Frank

    Ivo Guest

    document.body.onunload=function(){
    if( document.activeElement &&
    document.activeElement.tagName &&
    document.activeElement.tagName.toLowerCase()=='a' )
    alert(document.activeElement.href);
    }

    This will not work in every browser, and it will work incorrectly if user
    focuses an <a> then changes his mind and types a new url in the address bar,
    since the link will be the active element, but not the reason for the
    unload.

    Perhaps a better approach is to put an onclick handler on every <a> after
    all.
    This isn't too difficult however, with a loop through all links onload:
    for(var
    i=0;i<document.links.length;i++)document.links.onclick=somefunction;
    HTH
    Ivo
     
    Ivo, Jul 19, 2004
    #2
    1. Advertisements

  3. Play with this:

    var clicked; // this is a global variable

    function init() { // call this onload
    var links = document.getElementsByTagName('a');
    links[0].prototype.onclick = function() { clicked = this.href };
    }

    Something like that; the idea is that each click on a link will write it's
    href into a global variable -- on unload just check the global's value.
     
    Berislav Lopac, Jul 19, 2004
    #3
  4. Frank

    Frank Guest

    Thanks both, you sent me in the right direction. For the moment I decided
    on
    <SCRIPT FOR="hrefid" EVENT="onclick">
    alert('yep');
    </script>
    This way the <a> does not contain javascript and it serves my purpose.
    Frank
     
    Frank, Jul 19, 2004
    #4
  5. <head>
    ...
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    ...
    </head>

    <body>
    ...
    <a href="whatever.htm" onclick="alert('yep'); return false" ...>...</a>
    ...
    </body>

    serves it even better (and without an ID) than your proprietary IE nonsense.
    If you would have read the FAQ prior to posting, you would have known:

    <http://jibbering.com/faq/#FAQ2_3>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jul 25, 2004
    #5
  6. <head>
    ...
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    ...
    </head>

    <body>
    ...
    <a href="whatever.htm" onclick="alert('yep');" ...>...</a>
    ...
    </body>

    serves it even better (and without an ID) than your proprietary IE nonsense.
    If you would have read the FAQ prior to posting, you would have known:


    <http://jibbering.com/faq/#FAQ2_3>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jul 25, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.