Im not sure how to actually access the post variables in the url
First off, if they're in the URL, they're GET
variables, not POST
variables (which are in the body of a request sent to the server). Specifically, they're values on the query string.
You can access the query string via location.search
. Unfortunately, it is as it appears in the address bar, rather than nicely parsed out for you, but there are plug-ins you can use that will handle parsing it out for you, like this one which would give you access to your pg2
variable like this:
var pg2 = $.url(location).param('pg2');
That pg2
variable will be undefined
if there is no matching parameter on the query string.
That's just one example, there are several query string / URL parsing plug-ins available, or of course you can roll your own. Here's one I did a couple of years ago:
/**
* Split up the given string (for instance, window.location.search) into component parts.
*
* @param str The string
* @param The component parts as keys on an object; if the query string has repeated entries,
* that key's value will be an array; a key with no value is present with the value
* `undefined`.
*/
$.splitQueryString = splitQueryString;
function splitQueryString(str) {
var entries, parts, result, entryIndex, key, newVal, oldVal;
// We return the result as an object
result = {};
// Skip a leading ? if any
if (str.charAt(0) === '?') {
str = str.substring(1);
}
// Strip anything after the hash symbol
index = str.indexOf('#');
if (index >= 0) {
str = str.substring(0, index);
}
// decodeURIComponent won't do '+' => ' ', so do it
str = str.replace(/\+/g, ' ');
// Split into entries
entries = str.split('&');
for (index = 0; index < entries.length; ++index) {
parts = entries[index].split('=');
key = decodeURIComponent(parts[0]);
newVal = parts[1];
if (typeof newVal !== 'undefined') {
newVal = decodeURIComponent(newVal);
}
if (key in result) {
oldVal = result[key];
if ($.isArray(oldVal)) {
oldVal.push(newVal);
}
else {
result[key] = [oldVal, newVal];
}
}
else {
result[key] = newVal;
}
}
// Done
return result;
}
Usage:
var pg2 = $.splitQueryString(location.search).pg2;