kudago / smart-app-banner

Lightweight smart app banner with no jquery requirement
MIT License
526 stars 250 forks source link

Uncaught TypeError: Cannot read property 'appendChild' of null #9

Open kevin-hiq opened 9 years ago

kevin-hiq commented 9 years ago

I get the error : Uncaught TypeError: Cannot read property 'appendChild' of null

Smartbanner.js line: 189 "document.body.appendChild(sb);"

But "sb" does get declared a few lines higher up "var sb = document.createElement('div');"

Any idea what might be the issue?

CtrlF5 commented 9 years ago

just installed it, and this is the first error i have as well.

glibin commented 9 years ago

Could you provide a piece of code?

CtrlF5 commented 9 years ago

found that this is due to the body not being initialised yet as code is placed in head. Might be worth putting a document.readyStateChange before the code is run.

As a test i replaced line: 189

            document.onreadystatechange = function () {
              if (document.readyState == "complete") {
                document.body.appendChild(sb);
              }
            }

which works as it waits for the body to be available before adding the element.

glibin commented 9 years ago

Other way is to put smartbanner initialization right after body close tag

CtrlF5 commented 9 years ago

yes, i can place the code at the bottom, which i might choose to do so, but the example you have on install is then incorrect. so by taking your example the code fails without a catch for the the document to load.

iamstarkov commented 9 years ago

I faced this issue too

iamstarkov commented 9 years ago

right now README contains relevant example. prob this issue should be closed