javascript - How to use a var that has been set in one html file in another html file? -


i have html page sets value onclick using js function. need use value in html page. unable it. seems reset value when use value second page. .js file has been included in both html pages. how retain set value across multiple html pages?

within javascript itself, cannot keep track of variables across page loads. pass variable 1 page another, need use 'man in middle' out. either cookie, or query string.

a cookie work great! won't go specifics of setting/getting cookies in javascript there excellent documentation here: http://www.w3schools.com/js/js_cookies.asp

using set , methods (which of course customise suit needs):

first page:

var yourvariabletosave = 'i cheese , crackers'; var numdays = 10;    //how long cookie should last. setcookie('somecookiename', yourvariabletosave, numdays); 

second page:

var yourvariable = getcookie('somecookiename'); alert(yourvariable);    //displays 'i cheese , crackers' 

if want cookie last duration of current settings, can modfy setcookie function not specify expiry date.

if single request, query string might useful. this, append ?somequerystringname=yourvariablevalue links on page, or inject query string when user load page. won't go in depth option suspect cookie may bit better.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -