Tuesday 17 February 2015

Storage Events in javascript

Storage Events

The storage objects fire events which your appliation can listen for. A storage event is fired when you insert, update or delete a session or local storage property.
The storage event is only emitted in other browser windows than the one that inserted, updated or deleted the session or local storage objects.
For session storage this means that events are only visible in pop up windows and iframes.
For local storage, events are visible to all other windows open with the same origin, including pop up windows and iframes



           $(window).unbind('storage').bind('storage', function (e) {
                if (e.originalEvent.key === 'usersession') {
                    if (e.originalEvent.newValue !== e.originalEvent.oldValue) {
                        if (!e.originalEvent.newValue) {
                            window.location = '/';
                        }
                        else {
                                //console.log('storage change event : ' + e.originalEvent.newValue);
                        }
                    }
                }
                else if (e.originalEvent.key === 'requests') {
                       console.log(e.originalEvent.newValue);
                }
                else if (e.originalEvent.key === 'activities') {
                    if (e.originalEvent.newValue) {

                    }
                }
            });