在 iframe facebook 应用程序的内定义 JavaScript 函数标签

发布于 2024-08-15 04:11:36 字数 971 浏览 1 评论 0原文

如何在 的内部定义 JavaScript 函数?我尝试在结束标记上加载JavaScript文件,但仍然无法调用JavaScript文件中的函数。

这是 FBML 标签:

<fb:serverfbml>
  <script type="text/fbml">
    <fb:fbml>
      <a href="#" id="this" onclick="do_colors(this); return false">Hello World!</a>
      <script src="http://absolute.path.to/your/javascript/file.js"></script>
    </fb:fbml>
  </script>
</fb:serverfbml>

这是 file.js 中的 JavaScript 函数:

function random_int(lo, hi) { 
    return Math.floor((Math.random() * (hi - lo)) + lo) 
} 

function do_colors(obj) { 
    var r = random_int(0, 255), b = random_int(0, 255), g = random_int(0, 255); 
    obj.setStyle({background: 'rgb('+[r, g, b].join(',')+')', 
                        color: 'rgb('+[r<128?r+128:r-128, g<128?g+128:g-128, b<128?b+128:b-128].join(',')+')'}); 
}

我使用 Rails 和 facebooker 来开发应用程序。 对于包含 JavaScript 函数有什么想法或建议吗?

How can I define a JavaScript function on the inside of ? I tried to load a JavaScript file on the end tag , but I still can't call the function in the JavaScript file.

Here is the FBML tag:

<fb:serverfbml>
  <script type="text/fbml">
    <fb:fbml>
      <a href="#" id="this" onclick="do_colors(this); return false">Hello World!</a>
      <script src="http://absolute.path.to/your/javascript/file.js"></script>
    </fb:fbml>
  </script>
</fb:serverfbml>

And here is the JavaScript function in file.js:

function random_int(lo, hi) { 
    return Math.floor((Math.random() * (hi - lo)) + lo) 
} 

function do_colors(obj) { 
    var r = random_int(0, 255), b = random_int(0, 255), g = random_int(0, 255); 
    obj.setStyle({background: 'rgb('+[r, g, b].join(',')+')', 
                        color: 'rgb('+[r<128?r+128:r-128, g<128?g+128:g-128, b<128?b+128:b-128].join(',')+')'}); 
}

I use rails and facebooker to develop the application.
Any idea or suggestion for including JavaScript functions?

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

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

发布评论

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

评论(1

归途 2024-08-22 04:11:36

您是否尝试先加载脚本?
此处还有一些其他准则。
我知道如果你改变你的脚本,下面的内容会如何影响你。

由于缓存策略设置为从不
过期,您只需更新 URL
如果脚本的内容发生变化。
例如,您可以更改
http://foo.com/bar.js?v=1.0
http://foo.com/bar.js?v=2.0
http://foo.com/v1.0/bar.js
http://foo.com/v2.0/bar.js 当新的
该脚本的版本可用。

Did you try loading the script first?
There are a couple of other guidelines here.
I can see how the following could bite you if you change your script.

Since the cache policy is set to never
expire, you should update the URL only
if the content of the script changes.
For example, you can change
http://foo.com/bar.js?v=1.0 to
http://foo.com/bar.js?v=2.0 or
http://foo.com/v1.0/bar.js to
http://foo.com/v2.0/bar.js when a new
version of the script is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文