0

I have script like this:

<script language="JavaScript" type="text/javascript">

    function enter() {
        this.chrono = new Date().getMilliseconds();
    }

    function leave() {
        this.chrono = new Date().getMilliseconds() - this.chrono;

        alert("test" + this.chrono);

            var blockingRequest = new XMLHttpRequest();
            blockingRequest.open("GET", "visitor_log/ajax_store_visit_duration.php?visit_duration=" + this.chrono, false); // async = false
            blockingRequest.send();

        return null;
    }

    window.onload = enter;
    window.onbeforeunload = leave;
</script>

PHP part (visitor_log/ajax_store_visit_duration.php file):

<?php

if(isset($_GET["visit_duration"]))
{
    $text = $_GET["visit_duration"];

    logtxt($text);

    echo "OK";
}
else die("application error");

function logtxt($text)
{
    $myFile = "test.txt";
    $fh = fopen($myFile, 'wb');
    fwrite($fh, $text);
    fclose($fh);
}

?>

It works in Chrome perfectly, but it doesn't work in Opera.

How to make it cross-browser?

Kamil
  • 13,363
  • 24
  • 88
  • 183

1 Answers1

0

It shouldn't work anywhere. getMilliseconds() returns the milliseconds portion of the date object, not some ultimate milliseconds value. The value is always under 1000. Not useful in comparisons of your magnitude. What you really want is universal time.

(new Date()).valueOf() should get you a value you can work with.

This may be a partial answer, since you didn't actually specify what's not working.

  • Well, a little research )Google) turns up this discussion here on SO: http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in-firefox-safari-o . It might be relevant. –  Aug 02 '13 at 18:21