1

I've a HTML application that is running locally. When the first HTML page is opened it creates an array reading an XML file using jquery.

I am able to create the array successfully. Now I need to retain those array values when I browse through other pages of my application. It is because those array values contain imprortant CSS colors which will get applied to elements during runtime.

How can the data be passed between pages. Kindly advise.

Please Note: My application is running locally and not on a webserver.

2
  • Learn about localstorage Commented Oct 1, 2013 at 12:41
  • @epascarello hey m8 you faster then me :) I have just wanted to write this exact answer, lol. Commented Oct 1, 2013 at 12:42

3 Answers 3

1

Use localStorage

var yourDataArray=[];// your xml data.

//put data in Local storage against any key( say here XML_DATA) before navigation.

window.localStorage.setItem("XML_DATA", JSON.stringify(yourDataArray));

// now after navigation to another page ,get data using

var yourDataArray=window.localStorage.getItem("XML_DATA");
  if(yourDataArray!=null){
  yourDataArray=JSON.parse(yourDataArray);//here you will get data.
}
Sign up to request clarification or add additional context in comments.

Comments

1

Using web storage things like this are really easy now.

just do this in the js to save what you want:

localStorage.savedColor = "#330000";

and to retrive the savedColor from storage:

var mySavedColor = localStorage.savedColor;

Comments

0

You can accomplish this with a cookie:

https://developer.mozilla.org/en-US/docs/Web/API/document.cookie

Or on more modern browsers, use Storage.

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

For example,

if (window.sessionStorage) {
    var color_id = "ae5678";
    window.sessionStorage.setItem("color_id", color_id);
}

Or localStorage...

if (window.localStorage) {
     color_id = "6b67ce";
     window.localStorage.setItem("color_id", color_id);
}

To get the value,

 var color_id = window.localStorage.getItem("color_id");

1 Comment

Is it 1999? Cookie is not the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.