Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Trying to Calculate the Read Time For A Page

    I am trying to track the amount of time a user spends reading a specific webpage

    I have decided on using an onClose call to the function called closeTime()

    The code on the viewing page is:

    <script type="text/javascript" language="javascript">
    function closeTime() {

    var closeTime = getTime();
    var http;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    http = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
    http = new ActiveXObject("Microsoft.XMLHTTP");
    }

    http.open('GET', 'inc/savetime.php?closeTime=' + getTime(), true);
    http.send(null);

    http.onreadystatechange = function() {
    if (http.readyState == 4) {
    document.getElementById('some_div_id').innerHTML = http.responseText;
    }
    }


    }

    </script>


    </head>

    <body onClose="closeTime();">


    when I close this script NOTHING happens

    I even put an alert box in there and no alert box pops up .. I am using Firefox

    what am I doingwrong?
    Last edited by DrDel; 01-24-2006 at 05:33 AM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Use onunload. But the page may have finished unloading even before the script finishes executing. Try opening savetime.php in a small window onunload. Though popup blockers will block the popup.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry

    so I did this:


    <body onUnload="closeTime();">


    and it still doesn't execute when I close the browser

    I am using Firefox 1.5

    Javascipt is enabled since JS works on other sites

    can someone help me please?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The browser is being closed too quickly to record any of the info you are after. There is no way to get around this.

    They may have just left the page sitting there for half an hour during their tea break and remembered to close it when they came back so even if you did record a duration it will be meaningless.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    browser closing too fast?

    that is ridiculous!

    what would be the purpose of onCLose or onUnload then?

    it isnt executign for another reason

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The purpose of the unload even is to be able to clean up after you, and notifying other windows that you are being closed. The JavaScript environment will be intact, but there is no guarantee that the actual window object or any part of the DOM is still in place. Also, browsers may lock the environment so that you can't create new variables or change variables in that document since it's about to rip it apart.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    there must be SOMEONE out there who can suggest a way to solve this problem.

    How can one calculate the time it took a reader to read a webpage?

    any answers?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Just have a button at the bottom of the page that says [Done] which when clicked calls your closeTime() function.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall
    The browser is being closed too quickly to record any of the info you are after. There is no way to get around this.
    That is completely untrue. Sorry, it has to be said. An event should happen no matter what. I have tested what you have done (using my own ajax code) and I can get it to work in IE 5.5, IE 6.0 and FF 1.5, but Opera 8.51 is a bit more of a problem.

    QuirksMode
    In Opera load and unload do not fire when the user uses Back or Forward to enter or leave a page, or when the user closes the window.
    This severely hampers the usefulness of the events. When I write a load or unload script to, for instance, adjust the navigation of this site, I want it to run whenever the user enters or leaves the page, and not just when a browser thinks it's appropriate.
    Therefore I judge Opera's support Minimal and not Incomplete.
    The actual problem you are running into is that you are trying to get the info back. If you put the code (or code like it, as I ran) you get a JS error because you set a function to the 'onreadystatechange' which is causing an error.
    Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://xxx/javascript/AJAX.js :: CreateAJAX_Response :: line 118" data: no]
    Source File: http://xxx/javascript/AJAX.js
    Line: 118
    Granted I have a core routine that is called every time, but it is trying to set the 'onreadystatechange' which is causing an error. Actually this question helped me fix my code. Now when I send my core function a null I make sure to set the 'onreadystatechange' to null.

    I hope this helps. I would also like to make a suggestion of not sending a date and time string through the URL. Either post it, or just send the Milliseconds difference between the two times (onload and onunload). This way you make sure that you are getting an accurate reading each time. If you send a time to the server the servers clock might be different from the clients clock.

    Code:
    var startTime = null;
    window.onload = function () {
    	startTime = new Date();
    }
    
    //window.onbeforeunload = function ( ) {
    window.onunload = function ( ) {
    	var endTime = new Date();
    	var milDiff = Math.abs(startTime - endTime);
    	alert( milDiff );
    	//This is my function for my very core ajax control, don’t mind it for it does a lot of work in the background
    	//CreateAJAX('./ajax.asp?fun=testTime&value=' + milDiff , null );
    	
    }
    I noticed that my AJAX function was being called two times, I am sure this will be an easy fix and I do not have the time to do it. I hope this has answered your question correctly. I can be reached via PM if you have any more questions.
    Last edited by SpirtOfGrandeur; 01-26-2006 at 11:37 AM.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by SpirtOfGrandeur
    That is completely untrue. Sorry, it has to be said. An event should happen no matter what. I have tested what you have done (using my own ajax code) and I can get it to work in IE 5.5, IE 6.0 and FF 1.5, but Opera 8.51 is a bit more of a problem.
    QuirksMode
    Does it work consistently? It's all about timing. It may work sometimes or not. It depends on how long the processing in the server takes place. And if you have an alert in the onunload function, it may work more consistently as the script execution is delayed by the alert.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    How specific does the time have to be? You can have a timer that runs and reports every minute or so. You wouldn't have the exact time, but you could get within a minute.

  • #12
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    Does it work consistently? It's all about timing. It may work sometimes or not. It depends on how long the processing in the server takes place. And if you have an alert in the onunload function, it may work more consistently as the script execution is delayed by the alert.
    glenngv, I just put the alert in there to show him the number I was sending. I never run an alert in an onunload event. And I always wrap my onunload's in try/catches to make sure the user never has to deal with the inconvenience of a popup that happens when you try to close the window.

    Yes it works 100% of the time on FF and IE. The reason for this is that it can make the page call just before the browser unloads and does not care about the response. So in essence it makes a call to the web server then disconnects itself from it. I do not know why it is running 2 times though. It runs twice on both IE and FF. Then again it could be my code, but I have checked it toughly.

    This same theory also works with IFRAMES. You can do the same thing if the user does not have an XMLHTTPREQUEST Object. You can create an IFRAME instead and call a url and a load. Have a pause of about 20 (a for loop with a counter of 20) to make sure the IFRAME has had time to load (It is a slower connection then the XMLHTTPREQUEST Object). And it will also work.

    I have been using these objects for about 3 years now. I program full pages in AJAX (I love buzz words) all day long. I have so many widgets that use these objects. The one I have that is the closest related to this subject is a page load time (From the connection to the page ASP/PHP, until the page is done displaying on the client’s computer) to test my server on international connections. I have now made one that works to show how long some one has been viewing a page, thanks for the idea DrDel. I do use the onload, and onunload a lot (No I do not support Opera due to their limitations). I just thought of a hint I should share, to be safer you should use the onbeforeunload because there are some limitations to the onunload, but I highly doubt these limitations would be met by a timer script.

    Hopefully this helps!
    Last edited by SpirtOfGrandeur; 01-27-2006 at 03:54 AM.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #13
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I appreciate your code SpiritofGrandeur

    I tried it out using FF1.5 and MIE 6

    nothing happens when I close the browser.. same problem as with my code -- no alert window, nothing

    I don't know what the problem is... any other ideas?

    thanks again for your assistance, it is much appreciated. I have searched high and low for an answer to this problem -- no one seems to have the solution save for yourself

    so if this DOES work it would be a great success for a number of coders, not just me

  • #14
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    are tabs an issue with FireFox?

    If i have 6 tabs open an I close the one tab that is running the closeTime() script will it not work because I only closed the one tab ?

    For what it's worth I closed all the tabs and it still doesn't popup the Alert window.. don't know why

  • #15
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    block popups is also disabled in my browser, by the way...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •