Ashish
Ashish

Reputation: 63

Carry data within HTML pages using javascript or jquery

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.

Upvotes: 1

Views: 281

Answers (3)

andygoestohollywood
andygoestohollywood

Reputation: 1285

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;

Upvotes: 1

Anand Jha
Anand Jha

Reputation: 10714

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.
}

Upvotes: 1

JAL
JAL

Reputation: 21563

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");

Upvotes: 0

Related Questions