0

I have a web app, which has 2 html pages(html1,html2 related to javascript file js1.js,js2.js) When I click a button on html1, it will navigate to html2. I know there is the way transfer the parameter using url from html1/js1/js to html2/js2.js.

Is there a mechanism that set up variables both j1.js j2.js can access?(likes global variable in c)

Welcome any comment?

arachide
  • 8,006
  • 18
  • 71
  • 134
  • Related questions: http://stackoverflow.com/questions/1981673/persist-javascript-variables-across-pages, http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files, http://stackoverflow.com/questions/8416076/javascript-global-variables-shared-between-js-files – Ray Toal Oct 02 '12 at 07:48

4 Answers4

4

This is not directly possible as each page loads with its own window object namespace.

i.e. A global variable in js1.js (used in page1.html) is actually a member of the window object in page1. Similarly a global variable in page2.html is a member of that page's window object. The 2 window objects are totally different in the sense there is no site wide window or site object that can store global variable for use throughout a site

You can however use window.localStorage to share variables/values across pages in your site.

Example:

Setting value:

window.localStorage.setItem('myglobal', "hello");

Getting value:

var myglobal = window.localStorage.getItem('myglobal');
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 2
    Quick compatibility note: this isn't supported in IE7 or some mobile browsers, so if you plan to support these browsers then ensure this function is non-essential (I tend to work under the assumption that *all* JS should be non-essential anyway...) – Stu Cox Oct 02 '12 at 07:48
  • If IE7 and older are to be supported, I suggest you take a look at a cookie based solution. – techfoobar Oct 02 '12 at 07:50
1

Variables and values can be passed through pages via querystring. Have a look at this SO article.

If you strictly need to create a Javascript global variable, then you have to include a common snippet in both pages:

var myGlobalVar;

But this only represent a storage accessible by both scripts. You have to set its value anyway.

Community
  • 1
  • 1
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
  • The value of this global variable will still not travel across pages. I mean you can't set a value from page1 and get that value in page2 via the above method. – techfoobar Oct 02 '12 at 07:45
  • I get you. The query string method should work. Why don't you pass the value server side? – Alberto De Caro Oct 02 '12 at 07:51
1

A hackish solution is to store it into the window.name field. This is shared in the same window/tab of the browser. I personally don't like it. That variable isn't meant to be used this way.

There are libraries to persist state: http://pablotron.org/?cid=1557

Or, you could write your own code to store the variable in session (requires server-side programming), in HTML5 storage, in cookies, etc.

Giorgio Luparia
  • 846
  • 8
  • 9
0

You can either parse window.location.href to extract the parameters, set cookies in one page and retrieve them in the other (example here) which also requires parsing or use a server-side solution.

Most probably if you look for in interwebs you can find parsers for URLs and Cookies.

pedrofurla
  • 12,763
  • 1
  • 38
  • 49