Working with the storage Event
In this video we will introduce the ability to share immediate updates within multiple windows using the the storage event.
Video Transcript
00:00Alright, so you thought you were done with local storage, but actually you're not let's get back into the local storage
00:05Cuz there's something really fun and really cool that I want to show you how to read and really powerful by the way
00:11Alright, so let's see how it works basically
00:14Let's start with something really simple
00:15What I want to do is I want to create this text and make it editable now
00:18What would happen if this is is edible and we have a few windows open at the same time
00:22But we want all the windows to actually actually update as that copy is changed
00:28So let's see how we can make that happen
00:30So first of all, I'm going to go into my sample file and search for that h2 tag that we had our copy inside
00:36Here we go. And now we're going to add a new property and that property is content editable
00:42content editable
00:44And it's value we're going to set to be true. So now the content is editable now that our content is editable
00:51We could actually go into our application once we've refreshed it and then click anywhere in our title and maybe add here
00:57Maybe an exclamation point
00:59All right, so we're good at there
01:02Next what I want to do is I want to basically store the information as the information is being changed
01:07So let's go back into our application and this time around
01:10I'm gonna go into my storage event, which right now is completely empty
01:14And I'll just create a new variable that will grab our h2 title. So our h2 is going to be our document
01:22Doc you meant dot get LS the
01:28element by ID and
01:31We're gonna call our dynamic title. So we get access to our dynamic title and that's not document
01:40There we go, that makes more sense
01:41All right. So now that we got our document all that's left for us to do and you know
01:45Let's give this a name that you won't yeah, but we'll call it
01:49The h2 right dynamic h2
01:52Dh2 and for DHT what we want to do next is we want to add an event listener to it
01:57What is the event listener that we want to add is we want to add add an event listener for whenever our event changes?
02:05so whenever the user adds something into the element so add event listener and
02:10What we'll listen to is to key up because that is going to enable us to hear whenever a change happens
02:15Whatever the user picks up their keyboard whenever they're typing and a new letter has been dropped in and we'll just call it
02:21Let's call it on title change
02:24And let's just clean this up and make it nicely formatted. All right, so now let's create that function on title change
02:32It's gonna be an event. What do we want to do in that case? Literally all we want to do is go to our local
02:39storage and
02:40We're gonna update our H
02:43Dh1 let's call it right. Dh1. We're gonna update that with the
02:47D h
02:49Why D h1? D h2? We're gonna call it D h2 and we're gonna get that in our HTML
02:56So we're gonna store the change that just occurred
02:59Whenever it occurred something happened, we're gonna store it into our local storage
03:04alright, so the next step that we want to do is we want to actually have a
03:08Notification whenever our local local storage changed now the way we're gonna do that is also very similar
03:14It's very simple. We're gonna approach our window dot ad
03:20Event listener and we're gonna listen to storage
03:23whenever a storage
03:25Changes whenever our local storage will change it will update that storage property and we'll call the on
03:31storage change
03:33What do we want to do on storage change? Let's create a function for that on
03:38storage change
03:39What do we want to do in that case?
03:41Really what we want to do is we want to get the information that just got back to us to be dumped inside of our
03:49Actual title, so we're gonna go into our DH2 and we're gonna update its inner HTML
03:55To have the information that's inside of our local storage
03:59Because our local storage has been updated and this literally all we really had to do now if we go back into our
04:10Application and we look into Firefox and this time around we're gonna refresh our documents. Let me just make this a tad
04:17Smaller and I'm gonna grab this document and put it away in place where we'll be able to see the changes and just click on
04:23Refresh on that one as well
04:25And now that we have two documents one next to each other notice what happens whenever I go into my learning better is better
04:30And maybe I'm gonna change this title to
04:33It's time to start
04:36Learning notice how the copy automatically updates in the other window as well. And there you go
04:41We just created something really really really really cool. Now. We're more or less done with the topic of of local
04:48Events of storage events, but we're almost done
04:51But before we finish up with it
04:53I want to just take a couple of minutes in the next video to talk about the different properties of the storage event has
04:59All right, welcome back and we just changed and showed you how we could dynamically update our information
05:05the one next thing to note about this is that
05:08Why isn't it updating and internally as well as because the storage event happens only in other windows and it could be as many windows as
05:15You want so whenever you're updating something in this window
05:18It will automatically update any other window that is open with your site in it for that local storage information because it's listening to it
05:25All right. So now what I want to do next is before we wrap up this topic
05:28I want to go back into the editor and I just want to show you the different
05:32Properties that come back whenever this event happens, so I'm just going to do a console
05:37Log and we're not going to change anything in our in our example because our example is fantastic
05:45But instead of that, I'm just going to go back into my HTML document
05:48Maybe make it a little bit bigger and this time around we're going to focus on our
05:52JavaScript on the bottom here on our console, and I'm just going to change something here. Let's make let's just delete that. Oh
05:58Actually, we have to do it in another window, right? So I'm going to go into this window and
06:03Let's change something in here and automatically we got a storage object returning to us a storage event coming back to us
06:09Which has a few properties now?
06:11The only properties you really want to worry about or care about is the key itself that was changed
06:16Which is dh2 the new value that has changed which is the copy that has now been changed
06:22The old value was the value that was there before now
06:25There's more information that you don't have to worry about as much but for example
06:29So the location of the URL that changed it which might become important and the storage area itself
06:35So the storage area itself would actually have all the different information of the actual storage object
06:41Which in our case is our local storage which we saw that we created a few custom
06:45variables inside of it already
06:47So there you go
06:48That's all the information you really need to know to be able to do really really smart and really really cool things with the storage
06:55Object. So again, the key itself will return the key value itself that changed the new value will return the new value
07:01That was just returned the old value would be whatever was there before and also you would get the actual storage object itself
07:08Which you could then manipulate if you need last but not least was the URL itself of who changed it
07:13So you could know what page made those changes?
07:16Alright, and all this information could be could help you create really really sophisticated applications
07:22So I hope you enjoyed this and that's it
07:25that's all we have to talk about local storage and session storage for that matter because we now completed the topic of how do you
07:31listen to events of a
07:34local storage
