admin管理员组文章数量:1333710
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 .js:1:42020
So should I also check whether adsbygoogle
and also adsbygoogle.loaded
?
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./file.js:1:42020
So should I also check whether adsbygoogle
and also adsbygoogle.loaded
?
- Duplicate of this? – Paolo Gibellini Commented Aug 6, 2015 at 12:38
- 1 @Paolo Not quite - tha'ts adressing a single variable, rather than a nested property – James Thorpe Commented Aug 6, 2015 at 12:39
- The operation should be applied to all the interested elements. See the answers – Paolo Gibellini Commented Aug 6, 2015 at 12:42
4 Answers
Reset to default 11You need to check if adsbygoogle
is defined first:
var isAdsenseNotLoaded = !adsbygoogle || typeof adsbygoogle.loaded === 'undefined';
Yes, check for typeof adsbygoogle
first, this will return if the global variable adsbygoogle
is loaded.
var isAdsenseNotLoaded = (typeof adsbygoogle === 'undefined' || typeof adsbygoogle.loaded === 'undefined');
Checking for global variables with typeof
will never produce any exceptions due to trying to access an undefined variable. Reference: JavaScript check if variable exists (is defined/initialized)
So the whole object is not defined
var isAdsenseNotLoaded = (typeof adsbygoogle === 'undefined' || typeof adsbygoogle.loaded === 'undefined');
Just check in the first step if the object exists and then if it is loaded.
I'm going to guess that many people e here because they searched for OP's error message.
Putting window.
in front of adsbygoogle
solves the issue, because this way, if adsbygoogle
doesn't exist yet, it will be created.
(window.adsbygoogle = window.adsbygoogle || []).push({})
本文标签: adsenseJavascript checking whether a variable is undefinedStack Overflow
版权声明:本文标题:adsense - Javascript checking whether a variable is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742356924a2459599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论