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.

Lanshore
  • 43
  • 1
  • 1
  • 9
  • Which line is throwing that error? – Nick Zuber Apr 29 '16 at 20:14
  • Possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Aeolingamenfel Apr 29 '16 at 20:14
  • parr = parArr[i].value.split("="); – Lanshore Apr 29 '16 at 20:14
  • It's not a duplicate – Lanshore Apr 29 '16 at 20:15
  • If `parArr` is actually an array then you would just use `parArr[i].split("=")` – Jonathan Michalik Apr 29 '16 at 20:16
  • This is not a duplicate because I need to create that function and call the variable in a another function, besides my issue is that the script is showing me an error which is NOT RECOGNIZE the split!! THIS IS NOT A DUPLICATE! – Lanshore Apr 29 '16 at 20:17
  • I also tried ´parArr[i].split("=")´ but it still showing me the same error – Lanshore Apr 29 '16 at 20:18
  • check what is the value that `window.location.search` returns. Because if neither window.location.search nor staticURL have a `?`, it will crash when accessing the first position of the array – Bruno Apr 29 '16 at 20:19
  • @Lanshore No need to yell. Notice that they said "possible duplicate." And it's not the `split` isn't recognized, it's that you're trying to use `split` on `undefined`. It's the same thing as typing `undefined.split('=')`. – Mike Cluck Apr 29 '16 at 20:23

1 Answers1

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">
Shady Alset
  • 5,548
  • 4
  • 21
  • 34
  • 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 Apr 29 '16 at 20:35
  • @Lanshore no it's just for testing functionality, but where do you want displaying result ? – Shady Alset Apr 29 '16 at 20:42
  • Post this as answer, I cannot answer myself, I solved it by my own. – Lanshore Apr 29 '16 at 21:18
  • `function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) { vars[key] = value; }); return vars; } var first = getUrlVars()["pid"];` – Lanshore Apr 29 '16 at 21:18
  • This is a better way to get what I needed. – Lanshore Apr 29 '16 at 21:19