sascha
sascha

Reputation: 79

JQuery TypeError: object is undefined ( length = object.length, )

Everytime I use var.length I got an Error:

TypeError: object is undefined
length = object.length,

My JQuery Code:

function GetURLParameter(sParam) {
   var sPageURL = window.location.search.substring(1);
   var sURLVariables = sPageURL.split('&');
   for (var i = 0; i < sURLVariables.length; i++) {
       var sParameterName = sURLVariables[i].split('=');
       if (sParameterName[0] === sParam) {
           return sParameterName[1];
       }
   }
}

Meanwhile the URL:

login.html?error=autoLogout?from=request

Does anybody know how I can solve this Problem?

Upvotes: 0

Views: 575

Answers (2)

Qaddura
Qaddura

Reputation: 177

Your code is fine. However, the link you are using is incorrect.It should be : login.html?error=autoLogout&from=request

you can have a look at the link below also https://en.wikipedia.org/wiki/Query_string

Upvotes: 1

gaetanoM
gaetanoM

Reputation: 42054

You get the error because the url is wrong. It should be like:

login.html?error=autoLogout&from=request

Because the url is wrong when you try to get the 'from' parameter you receive an undefined so on an undefined if you try to get the length the system responds you with that message: TypeError: object is undefined.

If you do not want to change the url in order to adapt your function to your url you may do:

    function GetURLParameter(sParam) {
        var sPageURL = window.location.search.substring(1).replace('?', '&');
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] === sParam) {
                return sParameterName[1];
            }
        }
    }

Upvotes: 0

Related Questions