Reputation: 244
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?
Upvotes: 2
Views: 2078
Reputation: 3011
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...
Upvotes: 3
Reputation: 3856
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
Upvotes: 1
Reputation: 544
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.
Upvotes: 1