Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: .length is null

  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    .length is null

    For some reason i'm getting the error of "length is null". Which is from this.

    Code:
    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:
    Code:
    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

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Dormilich View Post
    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.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by martynball View Post
    Edit: FB says menu is undefined.
    that’s what I’d expect of a non-array.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    ??? No big mystery???

    Look:
    Code:
    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??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The way you need the values to be written requires rather an object. Try this:
    Code:
    //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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    martynball (07-26-2010)

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Dormilich View Post
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    martynball (07-26-2010)

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Thanks, figured out why it wasn't working. The ID was actually a element, not an ID.

    IE: function(this).

    Fixed it now thanks

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by Kor View Post
    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 = [];
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •