I’m trying to rewrite a snipped of PHP into JavaScript, ideally passing as few as possible variables between the two. Are there any super-global variables in JavaScript that give similar information to the PHP $_SERVER array?
In JavaScript, location seems to give me a bit of useful information but what about something like the PHP equivalent of $_SERVER['HTTP_REFERER']? I also have jQuery as a resource.
window.locationcan be read or set and comes with properties such as.pathname,.search,.protocol, et cetera — each of which will provide that particular aspect of the URL as a read-only value.document.referrerwill provide the referring URL as a string. If you want the domain/path/query/et cetera to be separated from one another, you will need to do that yourself, or use a library which will provide it.document.cookiewill provide you a semicolon-delimited list of user/server-set cookies.Again, turning that into an array or an object is on your own shoulders.
Your cookie string also has no access to expiration-times, nor applicable paths the particular cookie is set for — security.
For most of the rest of the data, you’re going to have to talk to the server — the browser likes to keep client-side script in the dark about things (like the user’s IP, or session-variables, or anything else which can be turned into a security-risk).