onunload action

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

    onunload action

    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


  • Ivo

    #2
    Re: onunload action

    "Frank" wrote[color=blue]
    > 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.[/color]

    document.body.o nunload=functio n(){
    if( document.active Element &&
    document.active Element.tagName &&
    document.active Element.tagName .toLowerCase()= ='a' )
    alert(document. activeElement.h ref);
    }

    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.link s[i].onclick=somefu nction;
    HTH
    Ivo


    Comment

    • Berislav Lopac

      #3
      Re: onunload action

      Frank wrote:[color=blue]
      > 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.[/color]

      Play with this:

      var clicked; // this is a global variable

      function init() { // call this onload
      var links = document.getEle mentsByTagName( 'a');
      links[0].prototype.oncl ick = 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.

      --
      If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
      Groucho, Chico, and Harpo, then Usenet is Zeppo.


      Comment

      • Frank

        #4
        Re: onunload action

        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
        "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message
        news:cdgi8o$anb $1@ls219.htnet. hr...[color=blue]
        > Frank wrote:[color=green]
        > > 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.[/color]
        >
        > Play with this:
        >
        > var clicked; // this is a global variable
        >
        > function init() { // call this onload
        > var links = document.getEle mentsByTagName( 'a');
        > links[0].prototype.oncl ick = 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.
        >
        > --
        > If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
        > Groucho, Chico, and Harpo, then Usenet is Zeppo.
        >
        >[/color]


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: onunload action

          Frank wrote:[color=blue]
          > 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.[/color]

          <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/#FAQ4_24>
          [color=blue]
          > [Top post][/color]

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


          PointedEars

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: onunload action

            Frank wrote:[color=blue]
            > 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.[/color]

            <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/#FAQ4_24>
            [color=blue]
            > [Top post][/color]

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


            PointedEars

            Comment

            Working...