faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

47 of 52 people (90%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Can I capture link clicks on document level?
Can I capture link clicks on document level?
Can I find out when the user is navigating to another page?

May 18th, 2002 20:48
Daniel LaLiberte, Martin Honnen,


With NN4 and IE4+ you can define
  document.onclick
to handle clicks on the document level. You can then check whether the 
event target/srcElement is a link by checking its href property. It is 
then possible to cancel the link action from the global onclick handler 
as the following example demonstrates:
<HTML>
<HEAD>
<SCRIPT>
if (document.layers)
  document.captureEvents(Event.CLICK);
document.onclick = 
  function (evt) {
    var target = 
      document.all ? event.srcElement :
        evt.target;
    if (target.href)
      return confirm ('Do you want to leave us for ' + target.href 
+ '?');    
    return true;
  };
</script>
</head>
<BODY>
<A HREF="http://www.kibo.com">
Visit GOD
</a>
<BR>
<A HREF="http://JavaScript.FAQTs.com">
JavaScript.FAQTs.com
</a>
</body>
</html>
But this mechanism of handling clicks at the document level does not
work for any element that explicitly handles clicks itself because, in
IE5 (and 6?), events start at the most specific element and bubble up to
the document at the top.  In NN6, events percolate down from the
document, so it is possible to catch events first at the document level.
 Perhaps a future version of IE will support the DOM standard that
specifies event handling both during percolation down and bubbling up.
------
IE4+ also introduced
  <BODY ONBEFOREUNLOAD="event.returnValue = 
                         'Are you sure you want to leave?';"> 
to allow for a confirmation dialog before leaving a page. Contrary to 
the above example on capturing (link) clicks this will fire event when 
the user types into the location bar.