0

I am doing an application and I believe html5 web storage, particularly localstorage, will be of great help to me. And surprisingly it is not that easy as I thought. Maybe I don't really understand the whole concept as I am not skilled in JavaScript for that matter.

Now below is the code I am using to call the JavaScript function in an external .js file.

ScriptManager.RegisterStartupScript(this, this.GetType(), "StoreUserLogin", "StoreUserLogin('" + this.LoginHiddenField.Value + "');", true);

Below the above code is the JavaScript function:

function StoreUserLogin(emailAddress) {
if (typeof (Storage) !== "undefined") {
    //var emailAddress = document.getElementById('LoginHiddenField');

    window.sessionStorage.emailname = emailAddress;
    alert("Information from sessionStorage:- " + window.sessionStorage.emailname);
}
else
    alert("Browser does not support storage");

}

Bainn
  • 37
  • 12

1 Answers1

0

first you should follow mozilla recommendations and use sessionStorage.setItem(); and sessionStorage.getItem();

But by the way you are not doing it the wrong way.

the first thing to know about sessionStorage it's not persistant , when the user will close it's browser all data inside will be wipe, not in case you use localStorage.

When you store something inside localStorage or sessionStorage, javascript perform a .toString() opération on data you want to save. So if you have to save a object, use JSON.stringify() before saving on sessionStorage to stringify your object the good way, and only after save this string inside sessionStorage;

look at this page for more informations: https://developer.mozilla.org/en/docs/Web/Guide/API/DOM/Storage

Edit: if it's not working in your if statement try to do this:

if (typeof (Storage) === "function")
babar78
  • 131
  • 7
  • @Bainn , is your c# ScriptManager.RegisterStartupScript registration working ? have try to put a `console.log()` or a `debugger`inside your javascript file to know if he is called ? if it's not look a this stackoverflow post on ScriptManager.RegisterStartupScript , because it's where is the problem. http://stackoverflow.com/questions/4994040/scriptmanager-registerstartupscript-code-not-working-why – babar78 Mar 03 '15 at 10:51
  • Hmmm! The point now is, like you suspected, the script is not being run at all. I have move the following code to the top section. But the same. How can I get the script to run? – Bainn Mar 03 '15 at 12:45
  • @Bainn when you register your RegisterStartupScript page already exist , you have to register this callback during the PreRender phase look at url linked inside my first comment , at the most voted answer – babar78 Mar 03 '15 at 13:28