I want to transfer a variable value without the need of cookies nor server interaction in JS, is ther any way possible ? for instance if i have foo.js and a variable fooVar = 10, and bar.js and barVar is there any way possible for barVar = fooVar without cookies?
-
Can you explain what you would like to achieve? Like, why can't it be the same file? And so on... – Mr.Web Jul 09 '13 at 16:46
-
So long as the variables aren't within a scope (global variables) fooVar will be available in bar.js assuming that the reference to bar.js comes after the reference to foo.js. – ArrayKnight Jul 09 '13 at 16:49
-
Are both JS files loaded into the same page? Sounds like an [XY-problem](http://meta.stackexchange.com/q/66377) btw – Bergi Jul 09 '13 at 16:49
-
1Ok, I want to carry a value from one page to the other, because depending on the value of the last page, some options on the next page will be available for the user – Ch32k0 Jul 09 '13 at 16:50
-
@Bergi no there are not loaded in the same page – Ch32k0 Jul 09 '13 at 16:51
-
@Ch32k0: And what's wrong with cookies or server interaction? They're made for exactly that purpose. Otherwise, you would just build a single-page-application and not navigate away from your page. By loading the second page, you have "server interaction" anyway – Bergi Jul 09 '13 at 16:51
-
@Bergi the app must be standalone and my supervisor wants me to do it that way. Later on we are making that app as you said, a single page. – Ch32k0 Jul 09 '13 at 16:55
-
@svillamayor can you show an example, I'm new to JS. Thank you – Ch32k0 Jul 09 '13 at 16:57
-
look at @jcdude answer and here: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – svillamayor Jul 09 '13 at 17:02
-
@Ch32k0: Then just make it a SPA right away. Or use cookies, they don't hurt until then. – Bergi Jul 09 '13 at 17:06
-
If you open the second page from the first page with w2=window.open() you get the reference to all the variable between the 2 pages. – donkeydown Jul 09 '13 at 17:10
3 Answers
Based on your comments, you could store the variable you want to access in the second page in the query string of the second page, or in the hash of the query string of the second page. Use that query string to load the second page.
e.g. in the query string: http://example.com/bar.html?myvar=5
or e.g. in the hash of the query string: http://example.com/bar.html#?myvar=5
Using the hash is likely to be the best option as your server will most likely ignore whatever you put in the hash (unless the server is programmed to do otherwise).
You can then use window.location.hash in the javascript loaded by bar.html to get the hash string and decode it to get your stored variable...
-
1
-
-
The code for this problem is pretty complex. You'll need some code to update a variable (or OBJECT) with the parameters based on whatever actions / buttons are being clicked. Then you'll need to apply those parameters to the URL, possibly in the HREF value of the link that takes you to the next page. Then, on the next page, you'll need some code to analyze the URL and parse out the parameters. In the pasted, I've used http://benalman.com/projects/jquery-bbq-plugin/ for this. – Joshua Jul 09 '13 at 17:10
-
You can use HTML5 localStorage
. The localStorage saves your data into the web browser. The difference with $_COOKIE is that data is accessible only via web browser with javascript and they are not sent in every HTTP request. Here is a link with examples.

- 524
- 1
- 5
- 15
You say that you want to carry the value of the variable from one page to the next page.
Why do you not use the url?
Add something like ?fooVar=10
to the url of the second page and then parse the url with the second script on the second page.
Here is a example how you can parse the url in the second script
how can i get query string values
-
Right approach, but the use of "?" means you have to use server-side. The guy suggesting hashes is more in line. – Joshua Jul 09 '13 at 17:07
-
@Joshua: No. The server does not need to use them. The JS on the clientside can read GET parameters as well. – Bergi Jul 09 '13 at 17:08
-
1@Bergi I agree, but at the same time "?" params get passed to the server, and depending on the server some will barf on unused params. Further, hashless URLS are harder to manipulate / change if on a single page (because they will cause refreshing etc.). Like I said, right idea, just not as ideal. – Joshua Jul 09 '13 at 17:12