beforeuloadはnamespace切っても複数発火しない

by FiNGAHOLiC

JavaScript

var beforeUnloadManager = {
    _queue: [],
    init: function(){
        var q = this._queue,
            i = 0,
            ret = [];
        $(window).on('beforeunload', $.proxy(function(){
            var l = q.length;
            for (; i < l; i++) {
                if (q[i].fn()) {
                    ret.push(q[i].mes);
                }
            }
            return ret.join('\n');
        }, this));
    },
    addQueue: function(mes, fn){
        this._queue.push({
            mes: mes,
            fn: fn
        });
    }
};

$(function(){
    beforeUnloadManager.init();
    beforeUnloadManager.addQueue('alert1', function(){
        return true;
    });
    beforeUnloadManager.addQueue('alert2', function(){
        return true;
    });
})