Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to detect browser close(x) click in firefox

Posted on 2010-11-17
4
Medium Priority
?
1,890 Views
Last Modified: 2012-05-10
I have an ASP.NET app
I need to detect when the user clicked the close(x) on the far top right.
I have an onbeforeunload event that triggers the code below. and have the following two questions

A)
This works for IE, Does Firefox recognize the following check, if not what is the equivalent
if ((window.event.clientX < 0) || (window.event.clientY < 0))

B)
Also what is the equivalent XMLHttpRequest object for Firefox

The firefox version I am testing with is
Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)
<script type="text/javascript" language="javascript">
    function logUser() {
        if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
      
             logoutXMLHTTP()
         }

     }
     function logoutXMLHTTP() {
         var xmlHttp;
         try {
             // Firefox, Opera 8.0+, Safari
             xmlHttp = new XMLHttpRequest();
             xmlhttp.open("GET", "AbandonSession.aspx", false); 
             xmlhttp.send();
         }
         catch (e) {
             // Internet Explorer
         try {
               xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
               xmlhttp.open("GET", "AbandonSession.aspx", false); 
               xmlhttp.send();
             }
         catch (e) {
             try {                                 
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
               xmlhttp.open("GET", "AbandonSession.aspx", false);
               xmlhttp.send();
                 }
                 catch (e) {
                     alert("Your browser does not support AJAX!"); return false;
                 }
             }
         }
     }


</script>

Open in new window

0
Comment
Question by:countrymeister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:deadlyDev
ID: 34154725
I know its not exactly an answer to your question... but check out http://jquery.com/ This library lets you write javascript code once (Not changing per browser) and takes care of the differences between browsers for you... Specifically check out the .ajax() method, which will use the XMLHttpRequest object from each of the browsers seamlessly... This framework changed my life!

When it comes to your problem code, you will need only write the code once, and it will work on all [supported] browsers!
0
 
LVL 6

Expert Comment

by:Codebot
ID: 34163197
If you are using asp.net then use server side session start and session end
they are more reliable
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/applications/state.aspx
or check this one
http://www.dotnetcurry.com/ShowArticle.aspx?ID=126&AspxAutoDetectCookieSupport=1
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34163357
Codebot,

Please note closing the browser is a pure client side event, and not a server side event.
I  need to know if what is the equivalent of window.event.clickX in firefox and the XmlHttp request object for Firefox
0
 
LVL 6

Accepted Solution

by:
Codebot earned 2000 total points
ID: 34163444
yes use
var e = (window.event) ? window.event : evt;

if (e.clientX < 0 && e.clientY < 0){
{

alert("window closing....");

}

Open in new window

and for object use
unction GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question