How does a page get the value of a variable in a js file modified by another page?

1. There is a data.js file that defines only one variable var project= {}
2.a.html reference data.js < script src= ".. / js/data.js" > < / script >. There is a function in a.html that gets the return value from the server and assigns it to the variable project

in data.js.
       browse:function (pro) {
            project=pro;//a.htmlporjectdata.jsproject
            console.log(pro);
            console.log(project);
            window.open("test.html");
        },
     

console print, pro,project, has value and is equal.
3.test.html also refers to data.js < script src= ".. / js/data.js" > < / script >, but jumping to test.html,test.html print project is empty in a.html. How to solve this problem?

clipboard.png

Mar.02,2021

//  pro
// a.html
window.localStorage.setItem('data',JSON.stringify(pro))

// test.html
var data = JSON.parse(window.localStorage.getItem('data'))

as for your question @ luozz the example has been given in great detail


use localstronge to solve


there are at least three ways
1. Save cookie
2. Deposit localStorage
3. Save it in the server session before fetching


from it.

on the face of it, it is as if I added the goods to the shopping cart in JD.com, and then opened Taobao, and found that it was the same reason why there were no items in the shopping cart. How can two completely independent pages share data in this way?

I see vue in the title tag. You can use components to pass values , vuex , Bus (new Vue) , etc., or to share data locally. Choose cookie , sessionStroage or localStroage according to your needs. There are many ways!
Menu