I was wondering how (if it's possible) to store information in the URL of a webpage? I've noticed in websites like Youtube there is information stored in the URL (for example which video in a playlist) and I would like to know how to do this for a website I'm working on.
-
JavaScript is a client side language. You would need to use PHP $_GET – user2896120 Jul 17 '16 at 02:27
-
[What you're referring to is called a query string](https://en.wikipedia.org/wiki/Query_string). – zzzzBov Jul 17 '16 at 02:28
-
What information do you want to store within URL? – guest271314 Jul 17 '16 at 02:29
-
You should read more about URL first to fully understand how it is structured and what parts it has https://en.wikipedia.org/wiki/Uniform_Resource_Locator You will find all your answers in the documentation. – anvk Jul 17 '16 at 02:32
-
1This is a perfectly legitimate question... I have no idea why there are so many downvotes and close votes. – Brad Jul 17 '16 at 02:40
-
@Brad, Yes, I have the same question, that's why I ended up here :) – Ben Viatte Jan 20 '21 at 04:03
1 Answers
Usually, the query string of the URL is used for extra data and parameters.
http://example.com/some-page.html?a=something&b=something%20else
Everything after the question mark ?
is the query string. This can be formatted however you want, but the most common way is the format of key=value
, with ampersands &
separating each variable.
Now, you might be wondering... what if you actually want an ampersand &
or some other non-alpha-numeric character? For that, you must use URL-encoding.
To access the query string in JavaScript, use window.location.search
. To actually parse the query string, see this: https://stackoverflow.com/a/901144/362536
You can set the query string in JavaScript if you want, by setting window.location.search
... just know that you're reloading the page if you do. Unless, you use push/pop state which is a common way to do this.
-
-
@Myk If you found this answer useful, consider marking it as the accepted answer and/or up-voting. – Brad Jul 17 '16 at 03:17