Access js variable of one file in another file using JQuery

后端 未结 2 792
遇见更好的自我
遇见更好的自我 2021-01-16 13:59

I have 2 js files: 1.js and 2.js.
In 1.js I have a variable

var test =\'Hello\';

I\'m trying to access the variable in 2.js

<         


        
2条回答
  •  半阙折子戏
    2021-01-16 14:58

    You can use sessionStorage to store the variable value

    $(document).ready(function () {
      sessionStorage.setItem('test ', 'Hello');
    });
    

    In the next file retrieve it using

    $(function () {
           function getData(){
             var data = sessionStorage.getItem('test');
                alert(data );
           }
       });
    

    Note the file 1.js need to be loaded before 2.js

提交回复
热议问题