I have a webpage that has several links that link to the same page, but depending on the link the user clicks I need to pass different info to that other page. I know how to do this in PHP through the URL and using $_GET[] but I am unsure how to do it using only javascript/jquery and html. Any idea? Thanks.
Asked
Active
Viewed 4,295 times
2
-
You could use a hashbang (`#!`) to store data, but that's a little sketchy. – royhowie Jul 14 '14 at 23:41
-
1javascript is stateless between page changes, choices are cookie, localStorage or url params. Javascript can set or get any of those – charlietfl Jul 14 '14 at 23:43
-
localStorage is persistent as Hell. Consider using sessionStorage if one doesn't like the query string solution. – Bob Brown Jul 15 '14 at 00:50
1 Answers
2
You can attach a query string to the link, just as you would to use $_GET with PHP, but retrieve the query string on the target page using JavaScript, as explained here: How can I get query string values in JavaScript?
Edit: I've looked some more at the page to which I linked. There's a lot of very general code there, but if you use an = sign and only one parameter, you can split off the parameter with a JavaScript split:
<a href="target.html?from=A">Came from A</a>
target.html's 'location.search' will contain "?from=A" and a simple split will do it.
-
2Also, note that this approach does not require JavaScript on the sending page, only on the target page. (If you have JavaScript on the sending page anyway, well, that's a horse of a different color.) – Bob Brown Jul 15 '14 at 00:52