简体   繁体   English

Facebook Canvas APP(Iframed)Auto-Height Resize

[英]Facebook Canvas APP (Iframed) Auto-Height Resize

Been running into an issue lately with Facebook canvas iframe applications. 最近使用Facebook canvas iframe应用程序遇到了问题。 I've set our settings to "auto-resize" and implemented the correct FB JS call to do the resizing of the height (to avoid unwanted scrollbars), but it doesn't seem to be working. 我已将我们的设置设置为“自动调整大小”并实现了正确的FB JS调用以调整高度(以避免不需要的滚动条),但它似乎不起作用。

Has anyone else had this issue or come up with a solution? 有没有其他人有这个问题或提出解决方案?

Thanks! 谢谢!

Erik 埃里克

Before </body> tag, I have written following code. </body>标签之前,我编写了以下代码。

<div id="fb-root"></div>
<script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({
                    appId: '<?php echo YOUR_APP_ID ?>',
                    cookie: true,
                    xfbml: true,
                    oauth: true
                });
                FB.Canvas.setAutoGrow(true);
            };
            (function() {
                var e = document.createElement('script'); e.async = true;
                e.src = document.location.protocol +
                    '//connect.facebook.net/en_US/all.js';
                document.getElementById('fb-root').appendChild(e);
            }());
</script>

And it is working fine for me. 它对我来说很好。

It doesn't matter whether height is Fixed or Fluid in Advanced application settings. 高级应用程序设置中的heightFixed or Fluid无关紧要。

FB.Canvas.setAutoGrow(true); was not working in my application, but I found that I missed the <div id="fb-root"></div> code. 我没有在我的应用程序中工作,但我发现我错过了<div id="fb-root"></div>代码。 I just put it before <script type="text/javascript"> and got the issue resolved. 我只是把它放在<script type="text/javascript">并解决了问题。

Expanding on a couple of points noted already: 已经注意到几点上的扩展:

window.fbAsyncInit = function () {
        FB.init({ 
           appId: 'YOUR APP ID', 
           status: true, 
           cookie: true, 
           xfbml: true, 
           oauth: true 
        });
        FB.Canvas.setAutoGrow(true);
    };

This will allow dynamic changes in the applications height. 这将允许应用程序高度的动态更改。 Also setting an explicit width in your css like so can help as well: 另外在css中设置显式宽度也可以提供帮助:

html {
   width: 760px;
   overflow: hidden;
}

Roozbeh is on the right path with his answer. Roozbeh的回答是正确的。 However, his code will most likely produce an error (at least in Firefox), as both the old and new SDKs are being referenced (and they don't play well with each other). 但是,他的代码很可能会产生错误(至少在Firefox中),因为旧的和新的SDK都被引用(并且它们彼此不能很好地发挥作用)。

Also, just using the new SDK to adjust the height is super simple: 此外,只使用新的SDK来调整高度非常简单:

<div id="fb-root"></div>
<script type="text/javascript">
    window.fbAsyncInit = function () {
        FB.init({ appId: 'YOUR APP ID', status: true, cookie: true, xfbml: true });
        FB.Canvas.setSize({ height: 890 });
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
            '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>

Official documentation available here . 这里有官方文件。

FB.Canvas.setAutoResize将被弃用,您应该使用FB.Canvas.setAutoGrow,因为它完全符合您的要求。

If anyone is still having this problem, its because your FB.init() is never being called. 如果有人仍然遇到这个问题,那是因为你的FB.init()永远不会被调用。 In my case, I had it inside my jQuery document.ready function. 就我而言,我把它放在我的jQuery document.ready函数中。 dont do that! 不要那样做!

Put this outside of your jQuery: 把它放在你的jQuery之外:

$(function() { ... jquery here... });

// do FB stuff
FB.init({appId: 'APP_ID', status: true, cookie: true });
FB.Canvas.setAutoGrow();

Auto-resize doesn't work. 自动调整大小不起作用。 It's a major bug that facebook has not fixed yet. 这是facebook尚未修复的主要错误。

However, here's the code work around for the problem: 但是,这是代码解决问题的方法:

<div id="fb-root"></div>
<script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({appId: 'YOUR APP ID', status: true, cookie: true, xfbml: true});
    };
    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
            '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>
<script type="text/javascript" src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"></script>
<div id='FB_HiddenContainer' style='display:none;position:absolute;left:-100px;top:-100px;width:0;height:0'>
</div>
<script type="text/javascript">
  window.onload = function(){
    FB_RequireFeatures(['CanvasUtil'], function(){
      FB.CanvasClient.setCanvasHeight('1500px');
    });
  };
</script>

Well ... auto-resize is buggy and only sometimes work 嗯...自动调整大小是错误的,有时只能工作

you can set it manually like this: FB.Canvas.setSize({ height: $('body').height() }); 您可以像这样手动设置它:FB.Canvas.setSize({height:$('body')。height()});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM