Persisting JavaScript Variables Across Pages
In JavaScript, variables generally have a limited scope to the page where they are defined. However, there might be scenarios where you need to access variables from one page to another. This is where the concept of persisting variables comes into play.
One method for persisting JavaScript variables across pages is to utilize the window.name property. This property allows you to store data as a string within the name attribute of the browser window. To set a variable using this method, assign it to the window.name object. For example, on Page A:
window.name = "someVar=5";
When you navigate to Page B, the value of someVar can be retrieved from window.name:
var someVar = window.name.split("=")[1]; // returns "5"
However, it's important to note that this approach has some limitations:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3