Talking about Window.onbeforeunload () event Call Ajax_jquery

Source: Internet
Author: User

Often there is the need to leave a Web page, the user does not necessarily log off, which will cause the session can not be destroyed in time. To implement the automatic logoff feature when a user leaves the page, you need to send a logoff command in the onBeforeUnload event handler for the Web page. This place is mostly implemented with Ajax. Sometimes it involves cross-domain access issues. There is a browser compatibility problem with this place.

The browser's incompatibility with this requirement is as follows: two.

1, processing Ajax when the incompatibility, here using jquery to solve.

2. Incompatibility when sending Ajax requests

The main code is as follows:

function logout () { 
        var logouturl = "xxxx";//to unregister the user's URL 
        if (Logouturl = "") return; 
        var useragent = Navigator.userAgent.toLowerCase (); 
        if (Useragent.indexof ("MSIE") >-1) {//ie 
          $.ajax ({url:logouturl, crossdomain:true, Async:false, DataType: " Jsonp "}); 
        else {//firefox Chrome 
          $.ajax ({url:logouturl, async:false}); 
      Window.onbeforeunload = function () { 
        logout (); 

Code Description:

Firefox in the processing of JS security level is high, many IE, chrome JS can be used in the Friefox is limited, so through

if (Useragent.indexof ("MSIE") >-1) {//ie
        }else {//firefox Chrome

This code to determine the current browser type.

The compatible code for Firefox and Chrome is as follows:

$.ajax ({url:logouturl, async:false});

The async needs to be set to false, that is, synchronous, and cannot be in a true asynchronous manner, otherwise the request may not be sent out. In fact, Chrome also applies to the following code for IE, when the browser will be automatically sent off the logout command, but click the browser's Refresh button when you want to automatically log off users, Chrome can only use the above line of code to issue a cancellation request

The compatible code for IE is as follows:

$.ajax ({url:logouturl, crossdomain:true, Async:false, DataType: "Jsonp"});

Crossdomain is set to True to resolve cross-domain access issues, and this property can be ignored if this problem does not exist. Async properties are best also set to False,true. DataType: "Jsonp" This property is also to solve cross-domain access problems, and crossdomain with the use of, there is no cross-domain problem, these two properties can be omitted.

The above code is passed in IE9, Chrome27, Firefox21 tests.

The above is a small series for everyone to talk about Window.onbeforeunload () event invoke Ajax (title) all content, I hope we support cloud-Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.