2

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.

3
  • You could use a hashbang (#!) to store data, but that's a little sketchy. Commented Jul 14, 2014 at 23:41
  • 1
    javascript is stateless between page changes, choices are cookie, localStorage or url params. Javascript can set or get any of those Commented Jul 14, 2014 at 23:43
  • localStorage is persistent as Hell. Consider using sessionStorage if one doesn't like the query string solution. Commented Jul 15, 2014 at 0:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, 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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.