locked
LocalStorage via Content Page RRS feed

  • Question

  • User-1920727399 posted

    In testing, I have been able to read and write LocalStorage via a buttonclick. I'm trying to access a value in LocalStorage when a content page (About.aspx) loads. I'm unsure of the code needed to do this on a content page based off a master page.

    The ID is "StationId" and the value will be some small number i.e. 1, 2, 3 etc. When the page loads I want to show the StationId in a label something like "Station Id = #"

    At the next level I'll need to pass the StationId to a function in CodeBehind,

    I'm a novice at JScript and would appreciate any help.

    Tuesday, January 23, 2018 4:24 PM

Answers

  • User181930479 posted

    Hello Peak Creek

    You can do this using HTML 5 

    here is what to do :

    lets say you want to display the station id in a div (can be also in anything )

    first lets create a div :

    <div id="result"></div>

    then , add this to your page :

    <script>
    // Check browser support
    if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("StationId", "N") ;   // where N is retrieved from DB or session or whatever.
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("StationId");
    } else {
    document.getElementById("result").innerHTML = "this technology NOT Supported on your browser";
    }
    </script>

    if you want to create the div by javascript :

    var iDiv = document.createElement('div'),
      
    iDiv.id = 'result';
    iDiv.className = 'block';
    document.getElementsByTagName('body')[0].appendChild(iDiv);

    if you wan to t see the result in your browser , all browsers can display them , im using chrome . First right click on the page --> inspect --> Aplication --> storage --> localstorage--> and you will see the name of the page you are displaying , click on it and VOILA.

    you will see the Key and the value 

    key = stationid 

    value = N (the one you have assigned).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 6:17 AM

All replies

  • User181930479 posted

    Hello Peak Creek

    You can do this using HTML 5 

    here is what to do :

    lets say you want to display the station id in a div (can be also in anything )

    first lets create a div :

    <div id="result"></div>

    then , add this to your page :

    <script>
    // Check browser support
    if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("StationId", "N") ;   // where N is retrieved from DB or session or whatever.
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("StationId");
    } else {
    document.getElementById("result").innerHTML = "this technology NOT Supported on your browser";
    }
    </script>

    if you want to create the div by javascript :

    var iDiv = document.createElement('div'),
      
    iDiv.id = 'result';
    iDiv.className = 'block';
    document.getElementsByTagName('body')[0].appendChild(iDiv);

    if you wan to t see the result in your browser , all browsers can display them , im using chrome . First right click on the page --> inspect --> Aplication --> storage --> localstorage--> and you will see the name of the page you are displaying , click on it and VOILA.

    you will see the Key and the value 

    key = stationid 

    value = N (the one you have assigned).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 6:17 AM
  • User-1920727399 posted

    Excellent! Thank you for taking the time to help me.

    Wednesday, January 24, 2018 1:34 PM