What is the Use of StorageEvent storageArea Property in JavaScript
This article demonstrates the use of the StorageEvent “storageArea” property in JavaScript.
What is the Use of StorageEvent “storageArea” Property in JavaScript?
The “StorageEvent” comes with the “storageArea” read-only property that retrieves the storage object of the changed/modified storage item. It corresponds to the “storage” event handler that only fires when the window’s storage area changes. It is such that the storage event triggers when the other window changes except itself.
Syntax
According to the above syntax, the “storageArea” property does not require any additional parameter and returns an “Object” of the storage item.
Example: Applying the StorageEvent “storageArea” Property to Return the Object of the Changed Storage Item
This section shows the practical implementation of the “storageArea” property. In this example, it returns the object of the modified storage item.
HTML Code
Let’s, go through the given HTML code:
In the above lines of code:
- First, the “<body>” tag creates a body section aligned to “center”.
- Next, the “<h2>” tag defines a level 2 subheading.
- After that, the “<button>” tag creates a button with an attached “onclick” event to invoke the user-defined function “change()” on button click.
- Lastly, the “<p>” tag adds an empty paragraph with the id “demo” to display the storage object as the standard output.
JavaScript Code
Next, proceed with the below-stated code:
window.addEventListener("storage", myFunc);
function myFunc() {
var t = event.storageArea;
document.getElementById("demo").innerHTML = t;
}
function change() {
var x = window.open("", "myWindow", "width=150,height=150");
x.localStorage.setItem("time", Date());
}
</script>
In the above code lines:
- First, a “storage” event is associated with the newly opened window via the “addEventListener()” method to execute the function “myFunc()” upon the event trigger.
- Next, the function “myFunc()” is defined that defines a variable “t” to display the object of the storage item that is retrieved using the “storageArea” property.
- After that, it utilizes the “getElementById()” method to access the included empty paragraph via its id “demo” and display the object in it(paragraph) using the “innerHTML” property.
- Another function “change()” is defined that applies the window “open()” method to open a new window named “myWindow” with the specified width and height, respectively.
- The “change()” function also utilizes the “localStorage” property that sets the key named “time” with a current date as its value.
- Algorithm: It is such that the former function including the “storageArea” property returns the object upon opening a new window in the latter function upon the button click.
Output
The output displays the storage object upon opening a new window at the button click with the help of the “storageArea” property.
Conclusion
JavaScript provides a special “storageArea” property to retrieve the object of the changed storage item. It corresponds to the “storage” event handler that only fires when the window’s storage area changes. This guide provided a brief description of the JavaScript StorageEvent “storageArea” property.
Source: linuxhint.com