0

I am having problem with this error:

'undefined' is null or not an object'

Can you please have a look and let me know. In my coding, I want to have simple DOM JavaScript code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>
    init();
    function init()
    {
        getElementByTabIndex("4", "submit")[0].addEventListener("click", Verify, false);
    }
    function Verify() {
        alert('done');  
        // all verification code will be here...
    }
    function getElementByTabIndex(index, type, node)
    {
        if (!node)
        { 
            node = document.getElementsByTagName('body')[0]; 
        } 

        var a = [];
        els = node.getElementsByTagName('*'); 
        for (var i = 0, j = els.length; i < j; i++) 
        { 
            if (els[i].tabIndex == index && els[i].type == type)
            { 
                a.push(els[i]);
            } 
        } 
    return a; 
}

</script>
<body>
<input type="email" id="email" /><input type="password" id="pass" /> <label class="Login" for="login"><input value="Log In" tabindex="4" type="submit" id="login"></label>
</body>
</html>
7
  • Which line is causing the error? Commented Aug 12, 2011 at 17:55
  • When do you get the error? How do you reproduce it? On what line does it occur? Commented Aug 12, 2011 at 17:55
  • LINE 24 : getElementByTabIndex("4", "submit") 0].addEventListener("click", Verify, false); In IE, you can see JavaScript error in status bar or in Safari - the error show as 'TypeError: 'undefined' is not an object (evaluating 'node.getElementsByTagName')' in line 24. Commented Aug 12, 2011 at 17:56
  • it looks fine to me. I tested it in google Chrome (jsfiddle.net/DavidLaberge/gdgFS/2). What browser are you using? Commented Aug 12, 2011 at 17:58
  • Firefox and Google Chrome works i guess. but in Safari and IE is giving me that errors. Commented Aug 12, 2011 at 18:00

3 Answers 3

3

You have to move you code at bottom or call init() after body is loaded.

Reason: you are trying to get elements even before they exists.

Eg :

<head>
   <script>
      var elm= document.getElementById('id');

      //this will be always undefied, as trying to read element even before they exist
   </script>
</head>

<body>
  <div id='foo'></div>

   <script>
      var elm= document.getElementById('id');

      //this wont be undefined 
   </script>

</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thanks it works in simple HTML, i am trying to use in my extension now. I will let you know how it comes out in extension.
0

You call:

if (!node) { 
    node = document.getElementsByTagName('body')[0]; 
} 

But your script runs before the DOM has finished loading, and so the body tag does not exist.

So node is undefined, and when you attempt the following, you get your error:

node.getElementsByTagName('*');

Run init() on document load, instead of immediately.


PS. jsfiddle and Firebug allowed me to debug this very quickly.

Comments

0

'body' isn't available to javascript at the time you are trying to call init().

call your init method when the dom has finished loading, like so:

window.onload = function (){
    init();
}

note that in order to make this work across browsers (if you plan on using it outside your planned Safari extention) you will have to do some extra work. more info: http://www.javascriptkit.com/dhtmltutors/domready.shtml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.