Take the 2-minute tour ×

I have an array name params in my script1.js file...

Now, there's another file page1.htmin the same folder as the script and I want to accomplish the following in this page

... header, body, etc
//code to get the array called params from script1.js

Now I cant use <script src="script1.js"> because script1.js is called in another page with document attributes and opens a couple of other windows but that's irrelevant...

I want to import the params array from a .js file inside the script tags in my new page. Is there a way??


The script1.js file starts like this

jQuery(document).ready(function () {
    path = //code to get path
    openpage = window.open(path+'page.htm'); 
        // some code
    }, 1000);
share|improve this question
If the script and the page have no relation (no direct import and no import through a child window of the page), then as far as I know you can't do this. However, there should be a way using cookies to pass the array from a page to another. –  Stock Overflaw Apr 1 '12 at 20:26
@StockOverflaw i think cookies can only store strings... is it possible to store arrays... i know i can use delimiters to join the array into a string and split the string back into the array but if my data contains anything, then that will create a big big problem.. so is there a way to directly store an array into a cookie... –  BAU Apr 2 '12 at 8:50
You are right. Using cookies is the simplest method and i got it working in no time... Thanks for the Suggestion. If you could post this as an answer, I will accept it and you can get the rep points... –  BAU Apr 2 '12 at 12:56

2 Answers 2

up vote 0 down vote accepted

To elaborate on my comment and make a final answer, the way to proceed (as you seem to have it working) is by using cookies

Basically, you may use setters and getters for cookies as described in this W3Schools article.

Then, to encode the array correctly in order to store it in a cookie: given you seem to use JQuery, you can either use serialize() or more directly params().

Glad it helped, hope this further information helps too ;)

share|improve this answer
exactly... works like a charm... to further this answer for future reference, you can use JSON for more complex structures like arrays as elements of an array... shouldn't be much difficult to learn provided you're already working with pretty complex structures already... :D –  BAU Apr 2 '12 at 18:43

The easiest solution would be to break apart the script that has the array in it. Put that in one script, and the remainder in another. Then import both in one page, and the array and your other code in the second page.

share|improve this answer
I don't quite follow... can you elaborate? –  BAU Apr 1 '12 at 20:59
I can rephrase... is there a way to import an object from another script... be it an array, var, int, or whatever? –  BAU Apr 1 '12 at 21:01
Maybe I'm the one confused. The script tags on your page do not create separate scopes, so if your array is declared globally (rather than inside some function) then it will be available everywhere. –  Scott Sauyet Apr 1 '12 at 22:53
refer to the edit in the question... now script1.js is called in another page and is relevant there... if i call it in page.htm it will open another page like itself and so on... is there a way to pass variables from a .js file which opens a page in a new window and then get the values somehow in the script tags of the new page... –  BAU Apr 2 '12 at 8:29
and yes, the array is declared globally... –  BAU Apr 2 '12 at 11:24

Your Answer


