Control the browser window close in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mageswar005
    New Member
    • Mar 2008
    • 72

    Control the browser window close in javascript

    hello guys,
    I dont want to close the browser window using javascript, is it possible to do this? i need to control the browser close window.
    regards,
    mageswaran
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can use onbeforeunload where supported, but use wisely. Note that it will also fire when going back/forward or reloading.

    Comment

    • mageswar005
      New Member
      • Mar 2008
      • 72

      #3
      hello sir,
      i use onbeforeunload function, its working fine, it can control the browser
      close only, but i need to control Alt + F4 also . If i use Alt + F4 to close means the browser should be closed, please give me the coding and some idea.
      Regards,
      Mageswaran

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        So you don't want the onbeforeunload to fire when Alt-F4 is pressed?

        Comment

        • mageswar005
          New Member
          • Mar 2008
          • 72

          #5
          i need to fire when Alt-F4 is pressed , and also my coding is not working in mozilla firefox i dont know why? my coding is below

          [CODE=JS]
          window.onbefore unload=function (){
          if (window.event.c lientY < 0 && (window.event.c lientX > (document.docum entElement.clie ntWidth - 5) || window.event.cl ientX < 15) || window.event.ke ycode==115 ) {
          if(sessid) {
          event.returnVal ue="This will end your session.";
          }

          }
          }

          function closeme(sessid) {
          if (window.event.c lientY < 0 && (window.event.c lientX > (document.docum entElement.clie ntWidth - 5) || window.event.cl ientX < 15) ||window.event. keycode==115 ) {
          if(sessid) {
          window.open ("logout.php ,
          "mywindow","wid th=2,height=2") ;
          }
          }
          }
          [/CODE]

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            The code is very IE-specific which is why it won't work in other browsers. To use onbeforeunload where supported, use return:
            Code:
            window.onbeforeunload = function() {
                return "some text";
            }
            window.event is IE syntax; for other browsers, use the event passed as a parameter to the function. Finally, the clientX/Y < 0 hack works only in IE.

            Comment

            • mageswar005
              New Member
              • Mar 2008
              • 72

              #7
              hello sir,
              Please give me the firefox coding

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                What do you want to do on window close?

                Comment

                • mageswar005
                  New Member
                  • Mar 2008
                  • 72

                  #9
                  Thank you for your immediate reply sir

                  Originally posted by acoder
                  What do you want to do on window close?
                  Thank you for your immediate reply sir, i am appriciate you to reply me the correct way.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    I'm not sure if you mean the problem is solved or you still want a solution.

                    Anyway, if it's the latter, my question was quite valid. If you tell us why you want to detect the browser close event, then we may be able to provide an alternative solution. For example, if it's because you want to log out if the user closes the browser, you could use a session timeout instead.

                    Comment

                    • mageswar005
                      New Member
                      • Mar 2008
                      • 72

                      #11
                      Originally posted by acoder
                      I'm not sure if you mean the problem is solved or you still want a solution.

                      Anyway, if it's the latter, my question was quite valid. If you tell us why you want to detect the browser close event, then we may be able to provide an alternative solution. For example, if it's because you want to log out if the user closes the browser, you could use a session timeout instead.

                      Hello sir,
                      In My Application online Test Modules should be conducted, at that time some student didnt take test and simply close the browser,so we didnt track the marks for that student.Can you please give me the solution for this.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        You can use a timer server-side by setting the time when the test begins. Any answers submitted after the time has elapsed is not counted. So if the student closes the browser and then reopens, it wouldn't make a difference. If they try to restart the test, it will continue from where they left off. If it's after the time has elapsed, they can't continue. No JavaScript required except to show the time remaining.

                        Comment

                        • mageswar005
                          New Member
                          • Mar 2008
                          • 72

                          #13
                          Originally posted by acoder
                          You can use a timer server-side by setting the time when the test begins. Any answers submitted after the time has elapsed is not counted. So if the student closes the browser and then reopens, it wouldn't make a difference. If they try to restart the test, it will continue from where they left off. If it's after the time has elapsed, they can't continue. No JavaScript required except to show the time remaining.
                          ok thank you sir,now i got some idea.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Cool. If you need help implementing it, then post back with your code.

                            Comment

                            Working...