Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: unload event for a aspx page

  1. #11
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by icymint3 View Post
    you can set an event handler for window.onunload (client side script), and then do an XMLHTTPRequest.
    javascript cannot update the database directly because it is a client side language, but you can make a Request for a page that will cause the database to update. say you had a leaving.aspx page that just updates the leave time in the database when it is visited.

    Code:
    function LeavePage()
    {
    	var req = null;
    	
    	try { req = new XMLHttpRequest(); } // creating for Firefox, Safari, IE7, etc.
    	catch (e){	
    		try { req = new ActiveXObject('MSXML2.XMLHTTP'); } // creating for later versions of IE.
    		catch (e) {
    			try { req = new ActiveXObject('Microsoft.XMLHTTP'); } // creating for early versions of IE.
    			catch (e) { return false; } // Could not create an XMLHttpRequest object.
    		}
    	}
    	
    	if (!req) return;
    	req.open("GET", "leaving.aspx", true);
    	req.onreadystatechange = function() {};
    	req.send("");
    }
    thats for starters...
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  2. #12
    keroed1 Guest

    Default

    Quote Originally Posted by icymint3 View Post
    javascript cannot update the database directly because it is a client side language, but you can make a Request for a page that will cause the database to update. say you had a leaving.aspx page that just updates the leave time in the database when it is visited.

    Code:
    function LeavePage()
    {
    	var req = null;
    	
    	try { req = new XMLHttpRequest(); } // creating for Firefox, Safari, IE7, etc.
    	catch (e){	
    		try { req = new ActiveXObject('MSXML2.XMLHTTP'); } // creating for later versions of IE.
    		catch (e) {
    			try { req = new ActiveXObject('Microsoft.XMLHTTP'); } // creating for early versions of IE.
    			catch (e) { return false; } // Could not create an XMLHttpRequest object.
    		}
    	}
    	
    	if (!req) return;
    	req.open("GET", "leaving.aspx", true);
    	req.onreadystatechange = function() {};
    	req.send("");
    }
    thats for starters...
    oh ok cool going to give this a try...................

    question req.send(""); allows you to send a string to the page u redirecting the user to?

  3. #13
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by keroed1 View Post
    question req.send(""); allows you to send a string to the page u redirecting the user to?
    yes... pass it like a query string
    ex. "username=Kodeci&isGhetto=1&getTheDrift=false"
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #14
    Join Date
    Nov 2004
    Posts
    42
    Rep Power
    0

    Default

    Or you could create a global.asax file in your application and put the code there. There is a section especially for “application shutdown” although I am not sure if it will work when your page is redirected. But you can play with it.


    i use this piece of code in all my internal application. See example below:


    Code:
    <%@ Application Language="VB" %>
    
    <script runat="server">
    
     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
           
    ' Code that runs on application startup 'Dim ex As Exception = Server.GetLastError 'If Not ex.GetType.FullName = "System.Web.HttpException" Then ' Error_Reporting.Report_Error(Server.GetLastError.Message, HttpContext.Current.User.Identity.Name) 'End If
    End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application shutdown End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when an unhandled error occurs
    Dim ex As Exception = Server.GetLastError If Not ex.GetType.FullName = "System.Web.HttpException" Then Error_Reporting.Report_Error(Server.GetLastError.Message, HttpContext.Current.User.Identity.Name) End If
    End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a new session is started ' Dim a As New ArrayList
    End Sub Protected Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs) 'check if user was authenticated End Sub </script>
    Last edited by Ghetto_Nerd; Dec 11, 2006 at 03:49 PM.

  5. #15
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    maybe my memory going bad but any thing with Application in its name (for ASP.NET) is not per User or per Session. that being said, there is no 100% hit to do what he wants to do so maybe this, or one of them other events (from Global.asax) might interest him.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  6. #16
    keroed1 Guest

    Default

    thanks guys i got it to work. i used a javascript to listen for an on window close event and then i opened a goodbye page thanking the user for visiting the website and behind the scenes i log all off the information i wanted to capture

Posting Permissions

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