javascript - Sending a request back to server in onbeforeunload event - Stack Overflow

When a user edits a record in my application I track that they have it 'locked' to prevent an

When a user edits a record in my application I track that they have it 'locked' to prevent another user from editing it until the first user closed the window (or saves).

I'm trying to send the request back to the server to unlock the record but it's not working in Firefox. The onbeforeunload event is called in script but it's not sending back the request to the server. Any ideas?

<body>
<script>
    window.onbeforeunload = function (e) {
        doUnlock();
    }

function doUnlock() {
    if (navigator.appName == 'Microsoft Internet Explorer') {
        // works in IE (with an IFRAME)
        utilFrame.location.href = "/unlock.do?recordId=" + recordId;
    } else if (navigator.appName == 'Netscape') {
        // None of this works....
        //window.location.href = "/unlock.do?recordId=" + recordId;
        var req = newXMLHttpRequest();
        req.open("GET", "/unlock.do?recordId=" + recordId, true);
        req.send();
    } else {
        // Works for chrome
        window.open("/unlock.do?recordId=" + recordId);
    }
}

</script>

When a user edits a record in my application I track that they have it 'locked' to prevent another user from editing it until the first user closed the window (or saves).

I'm trying to send the request back to the server to unlock the record but it's not working in Firefox. The onbeforeunload event is called in script but it's not sending back the request to the server. Any ideas?

<body>
<script>
    window.onbeforeunload = function (e) {
        doUnlock();
    }

function doUnlock() {
    if (navigator.appName == 'Microsoft Internet Explorer') {
        // works in IE (with an IFRAME)
        utilFrame.location.href = "/unlock.do?recordId=" + recordId;
    } else if (navigator.appName == 'Netscape') {
        // None of this works....
        //window.location.href = "/unlock.do?recordId=" + recordId;
        var req = newXMLHttpRequest();
        req.open("GET", "/unlock.do?recordId=" + recordId, true);
        req.send();
    } else {
        // Works for chrome
        window.open("/unlock.do?recordId=" + recordId);
    }
}

</script>
Share Improve this question asked Feb 19, 2012 at 16:25 sproketboysproketboy 9,47718 gold badges68 silver badges100 bronze badges 2
  • Not sure if that's a typo but you forgot a space between new and XMLHttpRequest(). – Tim Commented Feb 19, 2012 at 16:43
  • Yeah I should have included the space there. – sproketboy Commented Feb 19, 2012 at 16:51
Add a ment  | 

2 Answers 2

Reset to default 3

Add a space between new and XMLHttpRequest.

On page unload, any pending (AJAX) requests are *cancelled. If you have to send an AJAX request to the server on unload, use a synchronous request (not remended!!!):

    var req = new XMLHttpRequest();
    req.open("GET", "/unlock.do?recordId=" + recordId, false); // false
    req.send();

I do not remend the synchronous request, because the user interface blocks until the request has finished. That's not very user-friendly.

You should look into using a time based lock. When a user starts editing a record lock it with a timestamp. Every 30 seconds send a ping from the user that the record should still be locked. If there is no ping for 60 seconds unlock the record.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039538a4607729.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信