Results 1 to 5 of 5

Thread: Javascript Global variables over mult-pages

  1. #1
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Global variables over mult-pages

    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

  4. #4
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default More explaination

    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?

  5. #5
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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:

    Code:
    <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:

    Code:
    <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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •