我正在开发Grails项目。我想在视图中添加一个Facebook“发送”按钮,但它只是渲染一次。当我加载视图时,发送按钮就在那里。但是当我重新加载页面时,它就不再存在了。 我正在使用这个插件:Facebook SDK for Grails
facebook标签的行为与我在此处测试的javascript代码非常相似:
<!--This script works just fine. I reload the page
and it alerts every time-->
<script type="text/javascript">
alert("hello");
</script>
<!--But this is weird. It only executes once and does not alert like
the previous piece of codes-->
<facebook:initJS appId="${facebook.app.id}" xfbml="true">
// Put here any JS code to be executed after Facebook JS initialization
alert('Some JS code');
</facebook:initJS>
JS / HTML结果
<script async="" src="http://connect.facebook.net/en_US/all.js"></script><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div></div><div><iframe src="http://static.ak.facebook.com/connect/xd_arbiter.php?version=9#channel=ff02df41aa60ce&origin=http%3A%2F%2Flocalhost%3A8080&channel_path=%2Fidrop-web%2F%3Ffb_xd_fragment%23xd_sig%3Df18ffea5a36e82%26" name="fb_xdm_frame_http" id="fb_xdm_frame_http"></iframe><iframe src="https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=9#channel=ff02df41aa60ce&origin=http%3A%2F%2Flocalhost%3A8080&channel_path=%2Fidrop-web%2F%3Ffb_xd_fragment%23xd_sig%3Df18ffea5a36e82%26" name="fb_xdm_frame_https" id="fb_xdm_frame_https"></iframe></div></div>
很难解释。我不知道如何让它更清晰。希望有人以前遇到过这个,并给我一些建议。
___ 这段代码来自插件的视图:
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : "${appId}",
<g:if test="${channelUrl}">channelUrl : "${channelUrl}", // Custom channel URL</g:if>
cookie : <g:if test="${cookie}">true</g:if><g:else>false</g:else>, // enable cookies to allow the server to access the session
oauth : true, // enables OAuth 2.0
status : <g:if test="${status}">true</g:if><g:else>false</g:else>, // check login status
xfbml : <g:if test="${xfbml}">true</g:if><g:else>false</g:else> // parse XFBML
});
<g:if test="${autoGrow}">
FB.Canvas.setAutoGrow();
</g:if>
${body}
};
(function() {
var e = document.createElement("script");
e.src = document.location.protocol + "//connect.facebook.net/${locale}/all.js";
e.async = true;
document.getElementById("fb-root").appendChild(e);
}());
</script>
找到了解决方法!