FB共享插件,除非页面重新加载否则不显示,角度[重复]

时间:2021-10-27 19:07:19

This question already has an answer here:

这个问题在这里已有答案:

I am trying to use the facebook share plugin. It works fine if I reload the page, but if i go to the page without reloading then the "share button" does not appear. I have tried moving the facebook sdk code into my template and into the head but neither seem to do the trick. Any ideas?

我正在尝试使用facebook共享插件。如果我重新加载页面,它工作正常,但如果我没有重新加载去页面,则不会出现“分享按钮”。我已经尝试将facebook sdk代码移动到我的模板中并进入头部,但似乎都没有做到这一点。有任何想法吗?

this is the fb code that I have right below the opening tag

这是我在开始标记下面的fb代码

  <div id="fb-root"></div>
  <script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5&appId=xxxxxxxxxxxxxxxx";
  fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
  </script>

where i implement in my html template the fbUrl is defined in my controller

我在我的html模板中实现fbUrl在我的控制器中定义

    <div class="fb-share-button" data-href="{{fbUrl}}" data-layout="button" id='fb-share'></div>

1 个解决方案

#1


0  

The problem is the function runs immediately. It's an immediately invocable function expression (IIFE). If you put it in the head, it runs before any of the rest of the document. The only (probably) safe place to put it would be at the end of your document, before the body tag, when all the html elements have been loaded.

问题是该功能立即运行。它是一个可立即调用的函数表达式(IIFE)。如果你把它放在头部,它会在文档的任何其余部分之前运行。放置它的唯一(可能)安全的地方是在文档的末尾,在body标签之前,当所有html元素都已加载时。

If you notice, one of the call is to getElementById but no elements are loaded if you put that into the head and the ID element might not have been loaded if you put it any place else.

如果你注意到,其中一个调用是getElementById,但如果你把它放到头部就没有加载任何元素,如果把它放在任何其他地方,可能没有加载ID元素。

#1


0  

The problem is the function runs immediately. It's an immediately invocable function expression (IIFE). If you put it in the head, it runs before any of the rest of the document. The only (probably) safe place to put it would be at the end of your document, before the body tag, when all the html elements have been loaded.

问题是该功能立即运行。它是一个可立即调用的函数表达式(IIFE)。如果你把它放在头部,它会在文档的任何其余部分之前运行。放置它的唯一(可能)安全的地方是在文档的末尾,在body标签之前,当所有html元素都已加载时。

If you notice, one of the call is to getElementById but no elements are loaded if you put that into the head and the ID element might not have been loaded if you put it any place else.

如果你注意到,其中一个调用是getElementById,但如果你把它放到头部就没有加载任何元素,如果把它放在任何其他地方,可能没有加载ID元素。