"Unable to get value of the property 'appendChild': object is null or undefined" while appending script to IE
Asked Answered
M

1

7

When I try to append the following script to IE, I get this error:

"Error: Unable to get value of the property 'appendChild': object is null or undefined"

It works fine in Chrome, but when testing on IE9 this occurs. Can anyone tell me what the error is?

  // create script in document
    var fbScript = document.createElement("script");
    fbScript.type = "text/javascript";

    // make script source the facebook plugin
    fbScript.src = "http://connect.facebook.net/en_US/all.js#xfbml=1";



    // append script to appropriate tab
    document.getElementById('Tab' + tab_counter).appendChild(fbScript);
Mineraloid answered 24/8, 2011 at 9:47 Comment(2)
It clearly can't find Tab + tab_counterCementite
Raynos, not clearly. This is IE we're talking about...but an immediate obvious flag.Alonso
W
11

This can happen if your javascript code is running before all parts of HTML are loaded. Try to put this code in a function and fire it on an onload event. or use the more elegant jQuery onload event. It is something like this

$().ready(function () {
    var fbScript = document.createElement("script");
    fbScript.type = "text/javascript";

    // Make script source the facebook plugin
    fbScript.src = "http://connect.facebook.net/en_US/all.js#xfbml=1";

    // Append script to appropriate tab
    document.getElementById("Tab" + tab_counter).appendChild(fbScript);

});
Windowlight answered 24/8, 2011 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.