如何修复我在加载Facebook javascript-sdk时得到的错误

How to fix the error I am getting while loading the Facebook javascript-sdk?

本文关键字:错误 javascript-sdk Facebook 何修复 加载      更新时间:2023-09-26

我正在使用以下代码将Facebook javascript sdk加载到我的页面:-

           (function() {
            console.log('Hello World! From self executing function.'); 
            var e = document.createElement('script');
            e.async = true;
            e.type = 'text/javascript';
            e.src = document.location.protocol +  '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
            console.log('javascript sdk is appended into the fb-root element of the page.');
        }());

它正在正确加载,但我在控制台中得到以下错误:-

  Error: Permission denied to access property 'toString'
  [Break On This Error]     
   ...5(i(ca.getElementsByTagName('*')),'forEach',true,function(ka){if(!ea&&ka.getAttr...

如何解决这个问题?

Facebook javascript SDK通常会导致跨浏览器问题。为了解决这个问题,facebook自己已经集成了一个方法,即在FB.init()函数中添加一个通道url。

window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
  appId      : 'YOUR_APP_ID', // App ID from the App Dashboard
  channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
  status     : true, // check the login status upon init?
  cookie     : true, // set sessions cookies to allow your server to access the session?
  xfbml      : true  // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
 };

添加通道文件解决了跨浏览器问题。

channel.html文件的内容应该只有一行:

<script src="//connect.facebook.net/en_US/all.js"></script>

FB.init()中的channelUrl参数是可选的,但强烈推荐使用。提供通道文件可以帮助解决三个特定的已知问题。

  • 包含跨帧通信代码的页面可能会导致社交没有channelUrl的插件显示为空白
  • 如果没有提供channelUrl,并且页面包含自动播放音频或者视频,用户可能会听到两个音频流,因为页面有在后台跨域加载了第二次沟通。
  • 通道文件将防止在您的服务器端日志。如果您没有指定channelUrl,则应该指定删除包含fb_xd_bust或fb_xd_fragment参数的页面视图从你的日志中确保正确的计数。

channelUrl必须是与包含SDK的页面匹配的完全限定URL。换句话说,如果您的站点使用www提供服务,并且您修改了文档,则通道文件域必须包含www。域名,您必须制作相同的文档。也可以在channel.html文件中更改域名。

https://developers.facebook.com/docs/reference/javascript/