Matej Posted October 15, 2014 Share Posted October 15, 2014 Hi , how can i storage object in sessionStorage? function setIt(){var kek=document.getElementById("kek"); //text inputwindow[kek.value]={};window[kek.value].name = "name";sessionStorage.setItem(kek.value ,window[kek.value])}function getIt(){alert(sessionStorage.getItem(kek.value.name))} /this allerts null i also tried to parse it var one=sessionStorage.getItem(kek.value);var two=JSON.parse(one);alert(two.name) it also didnt work Link to comment Share on other sites More sharing options...
justsomeguy Posted October 15, 2014 Share Posted October 15, 2014 sessionStorage.getItem(kek.value).name Link to comment Share on other sites More sharing options...
Matej Posted October 15, 2014 Author Share Posted October 15, 2014 still undefined Link to comment Share on other sites More sharing options...
davej Posted October 15, 2014 Share Posted October 15, 2014 Hi , how can i storage object in sessionStorage? The same way you store a string. Are you able to store a string? Are you able to convert an object into a string? Link to comment Share on other sites More sharing options...
Matej Posted October 15, 2014 Author Share Posted October 15, 2014 (edited) got it , thought i had to parse it not both stringify and parse:/ , thanks Edited October 15, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted October 15, 2014 Share Posted October 15, 2014 I am unfamiliar with what you are doing here... window[elem.value]={}; I think it is a bad idea. Where did you learn this? Link to comment Share on other sites More sharing options...
Matej Posted October 15, 2014 Author Share Posted October 15, 2014 (edited) i think it was somewhere on codeacademy ,not sure if in their tutorials or on forum as response, what is so bad on it? (well yea in this case it have input.value as name but whats bad on it overall?) Edited October 15, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted October 15, 2014 Share Posted October 15, 2014 What is the value? You want to create a global with any name? Link to comment Share on other sites More sharing options...
Matej Posted October 15, 2014 Author Share Posted October 15, 2014 (edited) i don't understand what do you mean. var input=document.getElementById("input"); // text input and i set window[input.value]="hello world" => if input values is "one" than its window["one"] which is the same thing as var one="hello world" Edited October 15, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted October 15, 2014 Share Posted October 15, 2014 You take a text input and create a GLOBAL with the name of the text input and you don't see anything wrong with that? You are not assigning a text input to a global. You are creating a global that has the name of the text input? Link to comment Share on other sites More sharing options...
Ingolme Posted October 15, 2014 Share Posted October 15, 2014 You take a text input and create a GLOBAL with the name of the text input and you don't see anything wrong with that? You'll have to explain it. Nothing is obvious, it only seems that way to an experienced person. I only see one thing wrong with it: It gives the user the ability to make the program break by overwriting important variables. A simple solution is to only give the user access to a particular object. // At the beginning of the programvar globals = {};// Elsewhere in the programvar input=document.getElementById("input");globals[input.value] = "hello world"; Link to comment Share on other sites More sharing options...
davej Posted October 15, 2014 Share Posted October 15, 2014 I am always worried that I am misunderstanding, or simply unfamiliar with a technique, but this also has no checks to avoid invalid variable names? Link to comment Share on other sites More sharing options...
Matej Posted October 16, 2014 Author Share Posted October 16, 2014 You take a text input and create a GLOBAL with the name of the text input and you don't see anything wrong with that? I'm aware of this problem, i mentioned it and yea ingolme your way is better Thanks for answers Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now