I have this code:

<script type="text/javascript">  
     window.fbAsyncInit = function() { 
     FB.Canvas.setSize({ height: 6000 }); 
} 
window.fbAsyncInit();   
</script>

This resizes my iFrame correctly about 50% of the time. The other 50% of the time it does not resize and the following error appears on my console:

Uncaught ReferenceError: FB is not defined
window.fbAsyncInit:122
(anonymous function)

At first I thought this was an error with how I embedded the Facebook Javascript SDK, but then why does it work sometimes and not at other times?

有帮助吗?

解决方案

Might be "race condition" of some sort, so just wait until FB is defined:

window.fbAsyncInit = function FbAsynchInit() { 
     if (typeof FB != "undefined" && FB) {
         FB.Canvas.setSize({ height: 6000 }); 
     } else {
         window.setTimeout(FbAsynchInit, 10);
     }
} 

When FB won't be defined it will keep checking every 10 milliseconds, until it's defined.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top