...

View Full Version : .length is null



martynball
07-25-2010, 10:47 PM
For some reason i'm getting the error of "length is null". Which is from this.



function display_form(id) {
//Create some variables
var html = "";
var container = "form_container";
var menu = load_menu(id, false);

//Get id
for ( var i = 0; i < menu.length; i++ ) {
alert(i);
}

http = getHTTPObject();
http.onreadystatechange = function() {
if ( http.readyState == 4 && http.status == 200 ) {
document.getElementById(container).innerHTML = http.responseText;
} else {
document.getElemenyById(container).innerHTML = "Loading...";
}
}
http.open( "GET", "includes/forms.php?form=" + id );
http.send();

//Watch input fields
//login_listener();
}/*


Here is the function:


function load_menu(menu, return_d) {
//Availible menus
var data = [];
data [ 'login' ] = [ 'Login', 'Register', 'Recovery' ];
data [ 'settings' ] = [ 'Profile', 'Account', 'something' ];

if (return_d == false) {
return data[ menu ];
} else {
//Menu container ID's
var cons = Array('menuitem1','menuitem2','menuitem3');

for ( var i = 0; i < cons.length; i++ ) {
document.getElementById(cons[i]).innerHTML += data[ menu ] [ i ];
}
}
}


Extra info:
id = login
menu = login

Dormilich
07-25-2010, 10:56 PM
I’d like to check that out with Firebug …

as far as I can see, the length can be either a number or undefined, I don’t see where the null comes from.

martynball
07-25-2010, 10:57 PM
I’d like to check that out with Firebug …

as far as I can see, the length can be either a number or undefined, I don’t see where the null comes from.

null was from IE btw. I will look in FB

Edit: FB says menu is undefined.

Dormilich
07-25-2010, 11:56 PM
Edit: FB says menu is undefined.

thatís what Iíd expect of a non-array.

Old Pedant
07-26-2010, 06:00 AM
??? No big mystery???

Look:


function display_form(id) {
//Create some variables
var html = "";
var container = "form_container";
var menu = load_menu(id, false);
...
function load_menu(menu, return_d) {
//Availible menus
var data = [];
data [ 'login' ] = [ 'Login', 'Register', 'Recovery' ];
data [ 'settings' ] = [ 'Profile', 'Account', 'something' ];

if (return_d == false) {
return data[ menu ];
...

So the value of id is passed along to become the argument named menu in the load_menu function.

And you can see that the *ONLY* valid values for menu are "login" and "settings".

So if id is *ANY* value other than one of those two string, of course the result will be null.

Have you debugged to find out what the value of id is??

Old Pedant
07-26-2010, 06:03 AM
Oh...missed that you said

Extra info:
id = login
menu = login

Perhaps there is an extra space in the id value??? Or or or...

Where do you invoke the display_form() from??

Kor
07-26-2010, 01:06 PM
The way you need the values to be written requires rather an object. Try this:


//Availible menus
var data = {};
data['login'] = ['Login', 'Register', 'Recovery'];
data['settings'] = ['Profile', 'Account', 'something'];


Don't let spaces between the object and its key, nor inside the arrays.

Dormilich
07-26-2010, 01:21 PM
to elaborate on that, if data were an array data = []; its length after you defined the properties (login, settings) would still be zero. JavaScript Arrays are always numerically indexed, if you assign additional properties, you assign them to the Object part, not the Array part.

Kor
07-26-2010, 02:13 PM
to elaborate on that, if data were an array data = []; its length after you defined the properties (login, settings) would still be zero. JavaScript Arrays are always numerically indexed, if you assign additional properties, you assign them to the Object part, not the Array part.
Yes, except that, the way the it is is, OP does not use the length of data, it uses the length of one of data's elements (data['login'] or data['settings']). Now those elements are arrays, thus they have a length. :)

martynball
07-26-2010, 02:39 PM
Thanks, figured out why it wasn't working. The ID was actually a element, not an ID.

IE: function(this).

Fixed it now thanks :)

Dormilich
07-26-2010, 03:05 PM
Yes, except that, the way the it is is, OP does not use the length of data, it uses the length of one of data's elements (data['login'] or data['settings']). Now those elements are arrays, thus they have a length. :)

I was fully aware of that and I didnít doubt it either, I was just explaining why you proposed data = {}; over data = [];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum