A
A
amorphine2016-07-26 21:27:56
JavaScript
amorphine, 2016-07-26 21:27:56

Emulating an event from local storage works in Chrome, but doesn't work in Firefox - what's wrong?

The event listener for local storage fires when there is a change in local storage on other tabs and does not fire in the current tab, so it was decided to emulate the event. The following code works in Chrome:

var e = document.createEvent( 'StorageEvent' );
e.initStorageEvent('storage');
window.dispatchEvent(e);

But it doesn't work in FF with
TypeError: Not enough arguments to StorageEvent.initStorageEvent.
Following the documentation, I got to the call of the following construction
e.initStorageEvent('storage', true, false, storage_cell, 0, 0);

However, at least somehow it was not possible to force it to work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Myateznik, 2016-07-26
@amorphine

The correct construction of the initStorageEvent function is:

void initStorageEvent(
  in DOMString typeArg,
  in boolean canBubbleArg,
  in boolean cancelableArg,
  in DOMString keyArg,
  in DOMString oldValueArg,
  in DOMString newValueArg,
  in DOMString urlArg,
  in nsIDOMStorage storageAreaArg
);

Working example:
var e = document.createEvent( 'StorageEvent' );
e.initStorageEvent('storage', true, false, 'key', 'oldValue', 'newValue', location.href, window.localStorage);
window.dispatchEvent(e);

Read more on the Mozilla Developer Network (MDN) and the spec on WHATWG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question