Reputation: 43
I am trying to get the ULR parameter, but my code does not run because at debugging it shows me an error which is: Cannot read property 'split' of undefined
and I am not sure what I am doing wrong.
For example: default.html?pid=405
I need to get in a JavaScript variable the 405
value. This is the code i am using:
<script type="text/javascript">
function getUrlParameters(parameter, staticURL, decode) {
var currLocation = (staticURL.length) ? staticURL : window.location.search,
parArr = currLocation.split("?")[1].split("&"),
returnBool = true;
for (var i = 0; i < parArr.length; i++) {
parr = parArr[i].value.split("=");
if (parr[0] == parameter) {
return (decode) ? decodeURIComponent(parr[1]) : parr[1];
returnBool = true;
} else {
returnBool = false;
}
}
if (!returnBool) return false;
}
</script>
<script type="text/javascript">
function runDepo()
{
var idParameter = getUrlParameters("pid", "", true);
}
Can somebody help me? Thanks in advance.
Upvotes: 0
Views: 584
Reputation: 5714
Just remove .value
from parr = parArr[i].value.split("=");
function getUrlParameters(parameter, staticURL, decode) {
var currLocation = (staticURL.length) ? staticURL : window.location.search,
parArr = currLocation.split("?")[1].split("&"),
returnBool = true;
for (var i = 0; i < parArr.length; i++) {
parr = parArr[i].split("=");
if (parr[0] == parameter) {
return (decode) ? decodeURIComponent(parr[1]) : parr[1];
returnBool = true;
} else {
returnBool = false;
}
};
if (!returnBool) return false;
};
document.getElementById('Result').value = getUrlParameters("pid", "default.html?pid=405", true);
alert(getUrlParameters("pid", "default.html?pid=405", true));
PID : <input type="text" id="Result" placeholder="Result">
Upvotes: 2