javascriptadsense

Javascript checking whether a variable is undefined


To check whether adsense script is loaded or not, I use this:

var isAdsenseNotLoaded = (typeof adsbygoogle.loaded === 'undefined');

But from many users has this error in stack trace:

ReferenceError: adsbygoogle is not defined
    at http://example.com/file.js:1:42020

So should I also check whether adsbygoogle and also adsbygoogle.loaded ?


Solution

  • You need to check if adsbygoogle is defined first:

    var isAdsenseNotLoaded = !adsbygoogle || typeof adsbygoogle.loaded === 'undefined';