buni
buni

Reputation: 662

onLoad and onunload events not firing

<script type="text/javascript ">
<body onLoad="timeTracker._recordStartTime();">
<body onunload ="timeTracker._recordEndTime(); timeTracker._track(pageTracker);"
</script>

I am trying to use this code to record the time interval between load and unload of a page.

Unfortunately, it does not work. Why?

Upvotes: 0

Views: 2398

Answers (1)

Rob W
Rob W

Reputation: 348992

You may only specify one <body> tag. Also, it's not possible in HTML to specify plain HTML tags inside script tags.

Either add the handler through JavaScript, or merge the attributes in one <body>-tag. JavaScript is case-sensitive, so you should use window.unload (lowercase) instead of window.onLoad.

Use:

<script>
window.onload = function() {
    timeTracker._recordStartTime();
};
window.onunload = function() {
    timeTracker._recordEndTime();
    timeTracker._track(pageTracker);
};
</script>

OR (without <script> tags):

<body onLoad="timeTracker._recordStartTime();" onunload ="timeTracker._recordEndTime(); timeTracker._track(pageTracker);">

Upvotes: 6

Related Questions