0

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.

9
  • Which line is throwing that error? Commented Apr 29, 2016 at 20:14
  • Possible duplicate of How can I get query string values in JavaScript? Commented Apr 29, 2016 at 20:14
  • parr = parArr[i].value.split("="); Commented Apr 29, 2016 at 20:14
  • It's not a duplicate Commented Apr 29, 2016 at 20:15
  • If parArr is actually an array then you would just use parArr[i].split("=") Commented Apr 29, 2016 at 20:16

1 Answer 1

2

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">

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

5 Comments

I tried this, but should I have to code this alert(getUrlParameters("pid", "default.html?pid=405", true)); is there any way not to have to write it?
@Lanshore no it's just for testing functionality, but where do you want displaying result ?
Post this as answer, I cannot answer myself, I solved it by my own.
function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) { vars[key] = value; }); return vars; } var first = getUrlVars()["pid"];
This is a better way to get what I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.