Solved

Passing a javascript array of user created objects to another page

Posted on 2006-12-01
2
184 Views
Last Modified: 2010-04-06
Hi, is there a way to pass an array of user created javascript objects from one page to another?

Here is an example :
<script type="text/javascript">

function myobj(){
this.oneattr = 0;
this.arr = new Array();
}

var test_obj = new myobj();
test_obj.oneattr = "Testing1";
test_obj.arr[0]="Testing2";

var to_pass = new Array();
to_pass[0] = tobj;
</script>

I then want to pass the array "to_pass" to another page and have access to the different attributes of the objects contained in "to_pass".
Is this possible?

Thanks
0
Comment
Question by:rocketsbay
2 Comments
 
LVL 5

Accepted Solution

by:
dasmaer earned 125 total points
ID: 18053349
you can url encode and decode with escape() and unescape(), you can then append the string to a link or form action.

Try doing:

var query= escape(to_pass);

document.getElementById("someDivTag").innerHTML="<a href='http://someplace.org/page.html?obj='"+query+">link</a>";

And then decoding on the target page using unescape:

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  alert('Query Variable ' + variable + ' not found');
}

var to_pass = unescape(getQueryVariable(query));

I'm not certain the you can escape an object so you may be forced to serialise it into a list of strings, hopefully this can get you started.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18054121
If your object isn't too big, you might store it in a cookie.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jquery validation by class 12 52
How to orderly load jquery libs with requireJS? 3 79
Problem to run file 9 113
Node js - send response after all promises are resolved 1 56
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question