persist value between two pages using javascript [duplicate] - javascript

This question already has answers here:
Persist variables between page loads
(4 answers)
Closed 7 years ago.
i have a one html page in which i am storing few user selected values now and want to use these values on another html page.
i am using jquery in my module and i have already tried window.name and window.localStorage but they don't persist values between two pages.
so please help me to solve this problem.

If you don't want a cookies--and if you're directing from the first page to the second, how about passing the values as GET variables to the next page:
http://example.com/newpage.html?var1=blah?var2=blerg
then you can access that data with window.location.search.

You could use the "hash":
http://my.app.com/page2.html#name1=val1&name2=val2
The hash would be ignored by the server, keeping things "clean". The second page can read the hash from
window.location.hash
and then parse out the name/value pairs with some simple string/regexp/array manipulation.
If you wanted the hash to be "hidden", your second page could also then remove the hash from the URL - this would not result in another trip to the server - changes to hash only result in browser/client side behaviour.

If it's just a few values, how about cookies?

Store the value inside a cookie on the first page and and retrive it on the second. Its very easy with the Jquery Cookie plugin http://plugins.jquery.com/project/cookie

You would have to try and use cookies (assuming users are nice enough to enable those).
Here is a very useful link: http://www.w3schools.com/JS/js_cookies.asp

Finally i got pretty cool solution on
http://plugins.jquery.com/project/DOMCached

Related

Simplest way to store this to do list data even after browser closed? [duplicate]

This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Storing JSON data in browser memory
(3 answers)
Closed 4 years ago.
I want to make a web-based TO-DO list exactly like the one on the W3schools link below, however when I close the browser all the inputs and edit disappear and it reverts back to the original. Is it possible to easily store the values (if not I will quickly delete this Q)? What code can I add to make it save the data (perhaps localstorage?) each time or would I have to set up a php/mySql? No authentication required, whatever is the most straightforward way to do it.
The exact code is below. I prefer to host locally for simplicty but if necessary web based hosting server is also fine with me.
To Do List Example from W3 Schools
You can use Javascript LocalStorage to save data on browser close, only if it is being loaded on a domain that is accessed via HTTP.
Though, you can use a server-side database, such as mySQL to store data.

I need access to variables both in php and the page's javascript

I am working on a php/JavaScript web application that must perform many calculations using many values input by the user. There are several pages of inputs, and calculations using values input on previous pages are everywhere.
I have been passing the recently entered values between pages using $_POST, and storing them for use in a serialized class saved as a $_SESSION variable. One obvious way to pass values from PHP to the page for use by JavaScript is to populate the page with hidden form elements. JavaScript could use this data and modify it as necessary, then pass the values via POST.
I may have many such hidden elements, and I can't help but think that this is a good way to slow down the pages. Is there a better way to store this data between pages? Cookies?
Thanks!
SH
Search AJAX in google. It's a technic that helps your javascript to communicate with your php. First it is hard to learn the syntax but watching videos from youtube will do just fine.

Passing javascript variables between pages [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Persist javascript variables across pages?
how to exchange variables between two html pages
I am working on creating a "quiz" that allows users to answer questions, then passes their answer and current score to the next page for the next question. I used this article and was able to get the user's answer to pass through to the next window:
http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm
The issue is that this passes the information through a form. Once I receive the score on the second page, I need to increment the score if the answer was correct. I can't figure out how to pass a javascript variable with an html form.
I feel like this should be a relatively easy fix, I'm just stumped.
Any thoughts or advice would be much appreciated!
xoxo
There are two obvious ways to maintain state in the browser without requiring that the server remember it between pages:
Cookies
localStorage
The latter is trivial to implement, but is only available in HTML5.
Note that neither is intended to be secure - a determined page hacker could set either to whatever value they wish.
You can submit a form using get method <form method="get" then use javascript to parse params in url. Reference here:

Retrieve the complete URL from an iFrame [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I get the current location of an iframe?
Hi i want to get the complete URL, and with the methods i know i get the full url but of the iframe, and i want the 'final' document.location.href,
how can i get it?
i don't care if its php or js, but i'm guessing by my experiments that with PHp it's not possible...
If you want to know where you currently are:
Client side - javascript - you can use alert(window.location).
Server side - php - .$_SERVER["REQUEST_URI"] or $_SERVER['PATH_INFO'] depending on what you want.
Take a look at the top answer from this question.
It's important to note that it's (luckily!) not possible to get the URL of an iframe if the iframe's URL is from a different domain than the parent page. If you're trying to do that, you're probably out of luck.

Parse URL with Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I parse a URL into hostname and path in javascript?
I’m trying to parse the url of the page. For example the current page is location.href.
So the main page of my blog, if I use alert(location.href); it will return “http://diaryofthedead.co.cc/” in an alert box. If I use alert(location.href); on page two of my blog, it will return “http://diaryofthedead.co.cc/page/2” in an alert box. Is there any way to parse the URL to get the number at the end. Does anyone know how I could do that? Could I use wildcard or something, to do something like: location.href+”page/”+*; While * is equal to whatever follows “page/”, and then turn * into a variable?
You can use
var pagenum = location.pathname.match(/\/page\/(.*)/)[1];
It will extract anything past '/page/' in your URL;
Checkout this package jsuri
Or keep it simple http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
URI.js is a library for working with URLs. It can not only parse URLs, but also offers a simple fluent API (jQuery like) for modifying URLs.
Take a look at the documentation on the location object http://www.w3schools.com/jsref/obj_location.asp
You first want the "pathname" part, location.pathname

Categories

Resources