PDA

View Full Version : Javascript Global variables over mult-pages



JDev
09-27-2006, 07:55 PM
Hello,

I have a problem where I would like to have a global variable stay set to a certain value that I set in a function, accross multiple pages. So the first page is loaded and the variable is set to a value by a <body onload="init()"> call. The next page (same server/webpage) now wants to reference this variable. Is this even possible?

Example psuedo code

first.html
<script>
var globalVar;
function init()
{
globalVar = 1;
}
</script>

second.html
<script>
alert(globalVar)
</script>

...?
thanks

jscheuer1
09-27-2006, 09:05 PM
This can sort of be done a number of ways in javascript. None of the methods that I can think of are either secure or even reliable. For this type of functionality, server-side code would be far superior. How that would be done depends upon the server-side languages available on the host. Also upon which you use. PHP and asp/VBscript are the most common. Some hosts do not offer server-side languages or limit their use to certain types of accounts and or limit their functionality even when made available. What you would need to do (if possible on your host) is to set a server session variable on the one page and reference it from the various pages that might require it.

Try asking this question in the PHP forum or the asp forum. Find out if you have one of those languages available first. If you have one of them or another server-side language available, you could also find information on setting this type of variable on the web, using your favorite search engine.

ddadmin
09-27-2006, 09:37 PM
But if JDev is simply looking for a quick way to persist a variable across pages for the sake of a JavaScript that's on multiple pages for example, JavaScript cookies would more than suffice IMO. Why complicate the issue.

It'd help JDev if you could describe what you're trying to accomplish by persisting a variable.

JDev
09-27-2006, 10:12 PM
I was trying to not get too detailed about the information, but you wanted it so here I go.

I have a webcam that is set to take a snapshot of it's view every 3 seconds. I have that picture as part of my website header. I use javascript to refresh that image to make it update every two minutes. It is set to every two minutes because I have to be nice and support the dinosaurs that still use dial-up. Sooo, I have a link that users may press and have the picture update every 5 seconds. Now if a user presses the button to have the webcam update at the higher rate it changes and works fine. However, when a user changes to the higher refresh rate and clicks on another link that goes somewhere on my same website, the timing goes back to the slower speed. I am using php to automate the site, but in order to get server-side variables, or to set server-side variables I need to refresh the page which is not an option. Soo, that is why I am here to find out if there is anyway for a global javascript variable to be referenced accross multiple pages?

fyi, the website is www.ecs.csus.edu

questions? answers? Bueller? Bueller?

ddadmin
09-27-2006, 10:57 PM
I'd say JavaScript cookies should suit that need nicely. I'll just use the original example you gave to illustrate how that would be done via cookies:

First Page:


<script type="text/javascript">

function getCookie(Name){ //get cookie value
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function setCookie(name, value, days){ //set cookie value
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

var globalVar;
function init()
{
globalVar = 1;
setCookie("globalVar", 1, 30)
}

init()
</script>

Second Page:


<script type="text/javascript">

function getCookie(Name){ //get cookie value
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function setCookie(name, value, days){ //set cookie value
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

if (getCookie("globalVar")!="") //if cookie exists with name = "globalVar"
alert(getCookie("globalVar")) //alerts "1"
</script>

The cookie functions need to be on all of the participating pages, so you'll probably want to put them in an external .js file.