How is this evaluating as false?
When I step through this code, the values pictured result in cssLoaded = 0; From what I can see, the length is greater than 0 and cssStylesheet.sheet has properties so should be true, therefore cssLoaded should be set to 1, however it's not happening. I must be missing something here..?
function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
console.log('oops');
}
if (cssStylesheet.href == stylePath){
try {
if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 ){
cssLoaded = 1;}
else开发者_运维技巧 if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 ){
cssLoaded = 1;}
else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ){
cssLoaded = 1;}
}
catch(ex){ }
}
if(cssLoaded) {
//alert('cssloadedcomplete');
resetPops();
$('#video-overlay').show();
positionElements();
saveBizzmail();
console.log('try:' + tryCount)
} else {
tryCount+=1;
console.log('try:' + tryCount);
setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
}
}
There are three possible ways your code gets cssLoaded == 0;
if (cssStylesheet.href == stylePath)
evaluates tofalse
.- All three
if/else
statements evaluate tofalse
. - One of the
if/else
statements throws an exception and thus the otherif/else
statements aren't executed.
In troubleshooting your issue, you should verify that the first two are not the issue as those are probably just logic errors in the comparisons or the data coming into the function isn't what you expected it to be.
For the third issue, you can implement defensive coding to make sure that all three if/else
statements are checked by changing your code to this (you don't even need the try/catch any more because it allows your code to bypass some of the tests):
function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
console.log('oops');
}
if (cssStylesheet && cssStylesheet.href == stylePath) {
if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules && cssStylesheet.sheet.cssRules.length > 0 ) {
cssLoaded = 1;
}
else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText && cssStylesheet.styleSheet.cssText.length > 0 ) {
cssLoaded = 1;
}
else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ) {
cssLoaded = 1;
}
}
if(cssLoaded) {
//alert('cssloadedcomplete');
resetPops();
$('#video-overlay').show();
positionElements();
saveBizzmail();
console.log('try:' + tryCount)
} else {
tryCount+=1;
console.log('try:' + tryCount);
setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
}
}
The former check ensures that you avoid an NPE like situation (by checking that cssStylesheet.sheet
is not null).
You have to check for the existence of the element, otherwise an error may occur (cssStylesheet.sheet
is undefined
). An expression will evaluate to false when:
- The variable doesn't exist (
undefined
,null
,void 0
) - Equals
0
(zero) - Equals
false
- Equals an empty string (
""
,''
) - A Not-a-number value (
0/0
,NaN
)
Note that an empty object {}
evaluates to true, because it's an object.
Another example:
if(window.addEventListener) window.addEventListener("load", function(){}, true);
else if(window.attachEvent) window.attachEvent("onload", function(){});
else window.onload = function();
If the if-conditions were not present, the code will throw an error at older versions of IE, which don't have such a method called window.addEventListener
.
精彩评论