roundcrisis said:
McKirahan said:
"The onUnload event handler is used to run a function
or JavaScript code whenever the user exits a document."
"a function or JavaScript code whenever the user exits a document"? Are you
sure the author knows what they are talking about?
Which also completely ignores that onunload is not in any way a part of
ECMAScript or its implementations, of which JavaScript is only one.
This Web site should not be used or recommended as reference material.
[...]
Please trim your quotes as explained and recommended in the FAQ.
http://jibbering.com/faq/
[...]
So what would you say its a good way to go about this?
I'd say the proposed approach is OK to a certain extent here; however, the
description and the context of which it was embedded in was not. (We are
not talking about a feature of JavaScript or ECMAScript but about a
language-independent feature of an API of user agents [the DOM] that can be
used in HTML with intrinsic event handler attribute values, and in some DOMs
also with proprietary event handler properties [where the former should be
preferred as they are interoperable and not at all error-prone, much in
contrast to the latter.])
You have to take into account that the `unload' event occurs whenever the
user navigates away from the current HTML document resource, which includes
that they close the browser window in which that resource is displayed.
(In that sense, tabs and frames can be considered windows as well.)
If that would be OK for you, you should use the `onunload' event handler
attribute, as it part of the HTML 4.01 Web standard. Keep in mind, though,
that neither of this can work without enabled client-side script support, so
you should not rely on the results for any further processing (including
statistics).
I also would need to know what link (if any) making this happen
The `click' event occurs for a hypertext link when it is activated, and that
occurs before the `unload' event of the document, so you could store a
reference to the corresponding DOM object in a globally available property.
For example:
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var clickedLink = null;
function handleClick(e)
{
if (e)
{
var t = e.target || e.srcElement;
if (t)
{
if (/a(rea)?/i.test(t.tagName))
{
clickedLink = t;
}
}
}
}
function handleUnload(o)
{
// include read access to `clickedLink' here
}
</script>
</head>
<body onclick="if (typeof event != 'undefined') handleClick(event);"
onunload="handleUnload(this);">
...
<a href="foo">...</a>
...
I ve seen some examples where "onload" you loop through all the a
links avalable in the page and add an onclick event
This is called "Unobtrusive JavaScript" by some (mostly incompetent) people,
which is a misguided approach in most cases. It certainly is here: as the
`click' event bubbles and provides information about its (the event) target,
you can attach the corresponding event listener code to one high-level
element for which the event is supported, here the `body' element.
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow
http://www.w3.org/TR/html4/interact/scripts.html#adef-onclick
I would like to use a good solution but I m not familiar with JS
HTH
PointedEars