Results 1 to 7 of 7

Thread: [RESOLVED] Trying to retrieve a session variable in Javascript?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Trying to retrieve a session variable in Javascript?

    I have two html pages, NO ASPX. I use javascript like this:
    Code:
    sessionStorage.setItem("locale", "photos/BOP")
    To capture the value of "photos/BOP" and that works fine. I want to retrieve this value in the 2nd html page using javascript. I have my body tag set up like this:
    HTML Code:
    <body onload="getImages()">
    Here is the getImages function in the 2nd html page:
    Code:
    function getImages() {
        var fileextension = "jpg";
        var dir = sessionStorage.getItem("locale");
    
        alert(dir);
    }
    When the value is shown in the alert statement, it's NULL. What am I doing wrong
    Last edited by dday9; Jul 2nd, 2021 at 11:58 AM.
    Blake

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Trying to retrieve a session variable in Javascript?

    Any ideas on this???
    Blake

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Trying to retrieve a session variable in Javascript?

    Quote Originally Posted by blakemckenna View Post
    Any ideas on this???
    How are you setting the sessionStorage value? Is this running under a local web server? Are you using http or https to access the web pages?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Trying to retrieve a session variable in Javascript?

    What I suspect is happening is that the body's onload event is firing before the sessionStorage values are accessible.

    Does using this work:
    HTML Code:
    <!doctype html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>blakemckenna - example</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
        </head>
        <body>
            <h1>blakemckenna - example</h1>
            <p>locale: <span id="locale"></span></p>
    
            <script>
            window.addEventListener('load', function() {
                var locale = sessionStorage.getItem('locale');
                document.getElementById('locale').innerHTML = locale;
            }, false);
            </script>
        </body>
    </html>
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Trying to retrieve a session variable in Javascript?

    I actually figured it out by using localStorage.setItem & localStorage.getItem.
    Blake

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Trying to retrieve a session variable in Javascript?

    Keep in mind that localStorage and sessionStorage are two different things. Just make sure that you're using the right storage.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Trying to retrieve a session variable in Javascript?

    I know that localStorage will retain the value on your drive unless you clear it out afterward...which I am.
    Blake

Posting Permissions

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



Click Here to Expand Forum to Full Width