Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass an object through url in javascript as a single var

Tags:

jquery

object

url

how to pass a js object through url

i have a js object

var info = {
    status: 1 ,
    time: "10:00:00-12:00:00" ,
    weekdays: {
      "monday",
      "tuesday"
    }
};

i don't want to use serialize, i want something like "localhost/jstest/#test?info"

Is there any way i can achieve this

like image 946
user1400191 Avatar asked Jun 16 '12 11:06

user1400191


2 Answers

You are going to have to serialize it one way or another. You could do it in the way you suggest by storing a JSON string representing your object in e.g. sessionStorage, and passing the name of the key:

sessionStorage.setItem("info", JSON.stringify(info));
window.location("localhost/jstest/#test?info");

You can then access the data stored in that key:

var info = JSON.parse(sessionStorage.getItem("info"));
like image 155
James Allardice Avatar answered Sep 20 '22 03:09

James Allardice


You can create your own querystring with the hashtag, then get the data from the url/hashtag in your code to retrieve the data.

like this:

localhost/jstest/#info!status=1!time=10:00:00-12:00:00!weekdays=monday,tuesday

Then you can find more help on how to read the hashtag data here: Working with single page websites and maintaining state using a URL hash and jQuery

like image 41
Control Freak Avatar answered Sep 21 '22 03:09

Control Freak