window.close() question in Java Script

Discussion in 'JavaScript' started by iBoy, Jan 6, 2007.

  1. #1
    Hi every one,

    I have 1 question.

    I want to call 1 function when user press the window X button any idea how to call a javascript function when user press X Button?

    I appreciate your answers.

    Thanks!
     
    iBoy, Jan 6, 2007 IP
  2. jonnyfishman

    jonnyfishman Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Came up with this morning. Hope it helps.

    
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    function onKeyPress () {
    var keycode;
    if (window.event) keycode = String.fromCharCode(event.keyCode);
    else return true;
    if (keycode == "x") {
    alert("Please Click on the Submit button to send this");
    return false
    }
    return true 
    }
    document.onkeypress = onKeyPress;
    //  End -->
    </SCRIPT>
    </HEAD>
    HTML:
    Can't claim all the credit for the code, it uses components from Shawn Hafen's site along with that from msconline.
    The code itself only works for IE at the mo, the reason being the way in which the different browsers call the onKeyPress function and i can't work out how to sort it, sorry :rolleyes: .
    One thing that you may want to change to resolve this issue is the line:

    keycode = String.fromCharCode(event.keyCode);

    if this is changed to:

    keycode = window.event.keyCode;

    means that the "x" value called in the line:

    if (keycode == "x")

    has be to be in unicode.

    Let me know how it goes :) .
     
    jonnyfishman, Jan 6, 2007 IP
  3. iBoy

    iBoy Well-Known Member

    Messages:
    4,322
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    195
    Articles:
    1
    #3
    its not working....not showing me this "Please Click on the Submit button to send this" When i click on X button
     
    iBoy, Jan 6, 2007 IP
  4. jonnyfishman

    jonnyfishman Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    By the window X button do you mean on the bar at the top of the page?
    The script i posted was for a keypresws of the letter x, apologies if that was the case.

    Why do you need to call a function when the page is closed? Are you trying to send some data to another page or simply give a 'goodbye' sort of response to the window closing?

    If you are looking at the former the only way I could think of is using php otherwise the following srcript (courtesy of DHTML Lab) will pull up an alert or alternatively by changing the function winUnload allow you to do as you please.

    
    <HTML>
    <HEAD>
    <SCRIPT>
    
    window.onbeforeunload = winUnload;
    function winUnload(){
    	boxtext = "Are you sure you want to leave? \n \nReally!!!";
    	return boxtext;
    }
    </SCRIPT onbeforeunload>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    
    Code (markup):
    Laters
     
    jonnyfishman, Jan 6, 2007 IP
  5. iBoy

    iBoy Well-Known Member

    Messages:
    4,322
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    195
    Articles:
    1
    #5
    This function is working properly but this is not 100% what i need. but it working 50% of my need.

    I want when the user click on X button then 1 function call automatically where i can run the Update query for userstatus..

    Update query will update the database that userstats is now offline...


    Some thing like that...
     
    iBoy, Jan 8, 2007 IP
  6. adolix

    adolix Peon

    Messages:
    787
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can use something like this:

    <BODY onunload="javascript:winUnload()">

    of course, winUnload() shall be defined in a JS, a showed before...
     
    adolix, Jan 8, 2007 IP
  7. iBoy

    iBoy Well-Known Member

    Messages:
    4,322
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    195
    Articles:
    1
    #7
    I think this is not correct
     
    iBoy, Jan 8, 2007 IP
  8. adolix

    adolix Peon

    Messages:
    787
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #8
    sorry, misstyped... it should not be winUnload, but: onunload()

    look, i just uploaded here an example that i found. it works perfectly. open the page, then click your browser's home button (for example).

    i took it from here
     
    adolix, Jan 8, 2007 IP
  9. jonnyfishman

    jonnyfishman Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hey, glad the script i gave works if only 50% :D

    The script that you posted is a bit beyond me in terms of understanding but there are a couple of things that may help you sort it out.

    The function winUnloadis completely arbitary so you could in fact put your userOffline function there instead like this:

    
    <SCRIPT>
    window.onbeforeunload = userOffline;
    function userOffline() {
    
    <%
    Dim user,cn,rs,sql,tom
    user = Session("UserName")
    
    set cn=Server.CreateObject("ADODB.Connection")
    set rs=Server.CreateObject("ADODB.recordset")
    cn.Provider="Microsoft.Jet.OLEDB.4.0"
    cn.Open "E:/Sameer/data.mdb"
    rs.Open("Select * From Users Where UserName = '"&user&"' "), cn, 1, 1
    tom = "Offline"
    Sql = "Update Users Set UserStatus = ('"&tom&"') WHERE UserName = '"&user&"' "
    cn.Execute(Sql)
    %>
    
                 boxtext = "Are you sure you want to leave? \n \nReally!!!";
    	return boxtext;
    }
    
    </SCRIPT> 
    
    Code (markup):
    What language is the code snippet in the middle of the function? Are you sure that the code is ok and that it is compatibile with javascript, as the function works fine without it (is the <% supposed to be followed by something like the <?php function)?
     
    jonnyfishman, Jan 8, 2007 IP
  10. iBoy

    iBoy Well-Known Member

    Messages:
    4,322
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    195
    Articles:
    1
    #10
    Correct but some error.

    When i write the following code in my main.asp then the code automatically run without the X Button press that mean user offline without pressing the X button... :) BUT i want when the user click on X Button THEN this function call and update the userstatus in the database.. But this function automatically call when i open main.asp.

    Please help.

    Thanks!

    <SCRIPT>
    window.onbeforeunload = userOffline;
    function userOffline() {

    <%
    Dim user,cn,rs,sql,tom
    user = Session("UserName")

    set cn=Server.CreateObject("ADODB.Connection")
    set rs=Server.CreateObject("ADODB.recordset")
    cn.Provider="Microsoft.Jet.OLEDB.4.0"
    cn.Open "E:/Sameer/data.mdb"
    rs.Open("Select * From Users Where UserName = '"&user&"' "), cn, 1, 1
    tom = "Offline"
    Sql = "Update Users Set UserStatus = ('"&tom&"') WHERE UserName = '"&user&"' "
    cn.Execute(Sql)
    %>

    boxtext = "Are you sure you want to leave? \n \nReally!!!";
    return boxtext;
    }

    </SCRIPT>
     
    iBoy, Jan 9, 2007 IP
  11. jonnyfishman

    jonnyfishman Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ok, looks like the code is being triggred regardless of whether the onbeforeunload event is triggered. I can only think of two possible solutions the first is to use the onunload event (i'm not sure if the onbeforeunload function is supported by ASP) as follows:

    
    <SCRIPT FOR=window EVENT=onunload>
    
        alert("The onunload event fired for the window object.");
    </SCRIPT>
    
    
    Code (markup):
    this should fire only when the window closes, simply add you ASP code bfore the alert.
    The other solution may be to add the HTML elements into the code to trigger it as follows:

    <HTML>
    <HEAD>
    <SCRIPT>
    function closeWin()
      {
    
        event.returnValue = "Any string value here forces a dialog box to \
                            appear before closing the window.";
      }
    </SCRIPT>
    </HEAD>
    <BODY onbeforeunload="closeWin()">
     
    </BODY>
    </HTML>
    Code (markup):
    with the ASP between the { and the event line.

    Hope this helps :)
     
    jonnyfishman, Jan 9, 2007 IP