开发者

Problem with Facebook XFBML and Internet Explorer 8 asynchronous loading

开发者 https://www.devze.com 2023-01-09 09:59 出处:网络
I\'m trying to implement an XFBML comment box on a page.It works in Firefox and Chrome, but only sporadically in Internet Explorer 8.

I'm trying to implement an XFBML comment box on a page. It works in Firefox and Chrome, but only sporadically in Internet Explorer 8.

I get an 'FB is undefined' error when the page hits the FB.XFBML.parse('fb-stuff');. Do I need to check if the Facebook Connect script has finished loading before I try to parse the XFBML?

<script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({
            appId  : '117378991625799',
            status : false, // check login status
            cookie : false, // enable cookies to allow the server to access the session
            xfbml开发者_StackOverflow社区  : true  // parse XFBML
        });
    };
    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
        FB.XFBML.parse('fb-stuff');
    }());
</script>


You shouldn't need to call fb.xfbml.parse yourself, since you're already instructing facebook init to do that for you in the fbAsyncInit. It's useful mainly in situations when you add new facebook elements to the page after the initialization and want them to be parsed and rendered.

And yes, you should wait for the javascript library to load. That's what the fbAsyncInit is all about. You create the script element for the connect library and inject it into the dom in that anonymous function, but it takes a little while for the browser to load and evaluate the code, so FB isn't available straight away. When the script has loaded (and the FB object is available) it will try to execute the window.fbAsyncInit and then you can do whatever you need to.

0

精彩评论

暂无评论...
验证码 换一张
取 消