简体   繁体   中英

javascript alert like gmail chat on chat updation

我希望在新的聊天中弹出窗口,或者在像gmail这样更新聊天时弹出窗口。例如,如果我在其他选项卡上,并且gmail聊天已更新,那么它会显示在标题上。。我只希望这种类型的代码在我的数据库更新时显示或每隔10秒之后,一旦我单击它,它就不会一次又一次显示...。

I once write an plugin for html5 Notification, with it you can create an notification easily:

/**
 *  Notification
 *  @author:    ijse
 *  @require:   Chrome10+
 *  @params:    Same as webkitNotifications.create[HTML]Notification()
 *  @usage:
 *      new Notify("http://www.baidu.com").onshow(function() {
 *              alert("show");
 *          }).onclose(function() {
 *              alert("close");
 *          }).show();
 */
window.Notify = function() {
    var _params = arguments;
    // Validate arguments
    if(_params.length == 0) {
        console.error("Notify need at least one argument");
        return ;
    }
    // Check browser support
    if(!window.webkitNotifications) {
        console.error("Your browser does not support webkitNotifications feature!!");
        return ;
    }

    var _onclose, _onclick, _onerror, _onshow;
    var _notification, _replaceId, _showFlag = false;

    function bindEvents() {
        // Add event listeners
        // In W3C, display event is called show
        _notification.addEventListener("display", _onshow, false);
        _notification.addEventListener("click", _onclick, false);
        _notification.addEventListener("error", _onerror, false);
        _notification.addEventListener("close", _onclose, false);

        if(_replaceId)
            _notification.replaceId = _replaceId;
        // !!IMPORTANT&&WEIRD!! remove next line no events will work
        var t = _notification;
    }
    function createfn(permission) {
        // About permission on Chrome:
        //      PERMISSION_ALLOWED (0) indicates that the user has granted permission to scripts with this origin to show notifications.
        //      PERMISSION_NOT_ALLOWED (1) indicates that the user has not taken an action regarding notifications for scripts from this origin.
        //      PERMISSION_DENIED (2) indicates that the user has explicitly blocked scripts with this origin from showing notifications.
        if(permission == 0) {
            // If permission is allowed
            // Create notification
            if(_params.length == 1)
                _notification = window.webkitNotifications.createHTMLNotification(_params[0]);
            else
                _notification = window.webkitNotifications.createNotification(_params[0],_params[1],_params[2]);

            // Bind events
            bindEvents();

            // Show, if yes flag
            !!_showFlag && _notification.show();
        } else {
            if(_onerror)
                _onerror.call(this);
            console.error("Notification permission is denied!!");
        }
    }

    // If permission already allowed, do not require again
    if(window.webkitNotifications.checkPermission() != 0) {
        // Require permission from user
        window.webkitNotifications.requestPermission(function() {
            createfn.call(this, window.webkitNotifications.checkPermission());
        });
    } else {
        createfn.call(this, window.webkitNotifications.checkPermission());
    }

    // Return handler methods
    return {
        onclose: function(fn) { _onclose = fn; return this; },
        onclick: function(fn) { _onclick = fn; return this; },
        onerror: function(fn) { _onerror = fn; return this; },
        onshow : function(fn) { _onshow  = fn; return this; },

        show: function(replaceId) {
            _replaceId = replaceId;
            if(_notification) {
                // Notification already been created
                bindEvents();
                _notification.show();
            } else {
                // Flag yes to show
                _showFlag = true;
            }
            return _notification;
        },
        cancel: function() {
            _notification.cancel();
        }
    } // return handler
}

Instead of javascript you can use ajax function that calls a php page. use an array to accumulate all the messages. you said you are checking that page for every 10 seconds. display the array values in the chat window. then reset the array to empty. Then do the same process again. Hope this concept will work. I am not sure. If you want this in javascript. you should use the values in js array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM