如何在 window.fbAsyncInit 中调用 js 函数

发布于 2024-11-06 06:38:28 字数 3747 浏览 0 评论 0原文

我正在开发 Facebook 应用程序。我有以下代码,如果我像这样运行它,它可以正常工作

  <div id="fb-root"></div>
       <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, 
          status  : true, 
          cookie  : true, 
          xfbml   : true 

        );



          $('#stgame').click(sendRequest);
    function sendRequest() {
    document.getElementById('gameSetup').style.display = 'block';
        FB.ui({
            method: 'apprequests',
            message: '<?=$me[name];?> has invited you to a fun game of Towers. To play, just click on accept. Towers is a "3D" tile stacking word game.',
        },
        function (response) {
            if (response && response.request_ids) {
                var requests = response.request_ids.join(',');
                $.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {

                window.location.replace("play.php?" + resp);
                });
            } else {
               document.getElementById('gameSetup').style.display = 'none';
            }
        });
        return false;
    }


       };

      (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);
      }());
    </script>

,但是,我需要更改它,以便我可以将变量发送到“sendRequest”并将触发器更改为内联“onClick”

为此,我创建了一个链接在这样的页面上:

<a><img src=/images/start.png onClick=sendRequest('1234556');></a>

并将sendRequest函数更改为sendRequest(variable),以便它可以接受变量

但是当我这样做时,每次我单击具有onClick触发器的按钮时,它都会给我一个错误“找不到变量sendRequest”

我认为这是因为onClick看不到sendRequest函数。

所以我的问题是,如何从 onClick 调用该函数。请记住,页面上将有多个按钮需要调用该函数,为每个按钮赋予不同的变量值。

我当前的代码如下所示:

<a><img src=/images/start.png onClick=sendRequest('123');></a>
<a><img src=/images/start.png onClick=sendRequest('456');></a>

<div id="fb-root"></div>
    <script>


      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, 
          status  : true,
          cookie  : true, 
          xfbml   : true 
        }

        );


    function sendRequest(opponent) {
    document.getElementById('gameSetup').style.display = 'block';
        FB.ui({
            method: 'apprequests',
            to:opponent,
            message: '<?=$me[name];?> has invited you to a fun game" tile stacking word game.',
        },
        function (response) {
            if (response && response.request_ids) {
                var requests = response.request_ids.join(',');
                $.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {

                window.location.replace("play.php?" + resp);
                });
            } else {
               document.getElementById('gameSetup').style.display = 'none';
            }
        });
        return false;
    }



             };



      (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);
      }());
    </script>

如果有人能告诉我如何调用这个函数,我将不胜感激。据我所知,该函数需要保留在窗口内,fbAsyncInit 函数才能实际工作。

任何帮助将不胜感激。

I am working on a facebook app. I have the following code which works fine if I run it like this

  <div id="fb-root"></div>
       <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, 
          status  : true, 
          cookie  : true, 
          xfbml   : true 

        );



          $('#stgame').click(sendRequest);
    function sendRequest() {
    document.getElementById('gameSetup').style.display = 'block';
        FB.ui({
            method: 'apprequests',
            message: '<?=$me[name];?> has invited you to a fun game of Towers. To play, just click on accept. Towers is a "3D" tile stacking word game.',
        },
        function (response) {
            if (response && response.request_ids) {
                var requests = response.request_ids.join(',');
                $.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {

                window.location.replace("play.php?" + resp);
                });
            } else {
               document.getElementById('gameSetup').style.display = 'none';
            }
        });
        return false;
    }


       };

      (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);
      }());
    </script>

however, I need to alter it so that I can send a variable to "sendRequest" and change the trigger to an inline "onClick"

To do this I have created a link on the page like this:

<a><img src=/images/start.png onClick=sendRequest('1234556');></a>

and change the sendRequest function to sendRequest(variable) so that it can take the variable

However when I do this, each time I click on the button that has the onClick trigger, it gives me an error "cant find variable sendRequest"

I think this is because the onClick cant see the sendRequest function.

So my question is, how do I call that function from the onClick. bearing in mind that there will be multiple buttons on the page that will need to call the function giving the function a different variable value on each of them.

My current code looks like this:

<a><img src=/images/start.png onClick=sendRequest('123');></a>
<a><img src=/images/start.png onClick=sendRequest('456');></a>

<div id="fb-root"></div>
    <script>


      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, 
          status  : true,
          cookie  : true, 
          xfbml   : true 
        }

        );


    function sendRequest(opponent) {
    document.getElementById('gameSetup').style.display = 'block';
        FB.ui({
            method: 'apprequests',
            to:opponent,
            message: '<?=$me[name];?> has invited you to a fun game" tile stacking word game.',
        },
        function (response) {
            if (response && response.request_ids) {
                var requests = response.request_ids.join(',');
                $.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {

                window.location.replace("play.php?" + resp);
                });
            } else {
               document.getElementById('gameSetup').style.display = 'none';
            }
        });
        return false;
    }



             };



      (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);
      }());
    </script>

If anyone could tell me how I can call this function I would appreciate it. as far as I am aware, the function needs to stay inside the window,fbAsyncInit function in order to actually work.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-11-13 06:38:29

我认为问题在于 sendRequest 方法位于函数作用域内,因此在全局命名空间(dom 可以访问它的地方)中不可用。您可以尝试更改:

function sendRequest()...

window.sendRequest = function()...

还应该提到,几乎没有充分的理由像这样将事件直接附加到 dom。即使您在像 php 这样的模板语言中渲染 JavaScript 变量数据,最好以关联方式设置变量值,这种方式不会将事件耦合到 dom(但可以将数据耦合到元素)。

I think the problem is that the sendRequest method is inside the function scope, so not available in the global namespace (where the dom could get to it). You might try changing:

function sendRequest()...

to

window.sendRequest = function()...

I should also mention that there is almost never a good reason to attach events directly to the dom like that. Even where you are rendering that JavaScript variable data in a templating language like php it is far better to set the variable values in an associative way that does not couple the event to the dom (but could couple the data to the element).

三生池水覆流年 2024-11-13 06:38:28

首先,我没有查看你的所有代码,但我真的认为你应该在它上面付出更多的努力(格式和逻辑)。

无论如何,重点是:
您可以有一个标志,该标志仅在加载 JS-SDK 时设置为 true,并且您可以“阻止”window.fbAsyncInit 之外的任何进程,直到该标志变为放。像这样的东西:

<div id="fb-root"></div>
<script>
var isLoaded = false;
window.fbAsyncInit = function() {
    isLoaded = true;
...
...
}
function myFunc(myVar) {
    if(isLoaded) {
        // Do FB related stuff
    }
}

First of all, I haven't looked to all your code but I really think that you should do more effort on it (formatting and logic).

Anyway, to the point:
You can have a flag that will only set to true when the JS-SDK is loaded, and you can "block" any process outside the window.fbAsyncInit till the flag is set. Something like:

<div id="fb-root"></div>
<script>
var isLoaded = false;
window.fbAsyncInit = function() {
    isLoaded = true;
...
...
}
function myFunc(myVar) {
    if(isLoaded) {
        // Do FB related stuff
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文