Call global JS function from module .js file in Drupal 7(从 Drupal 7 中的模块 .js 文件调用全局 JS 函数)
问题描述
With the great help of Sk8erPeter I managed to execute Javascript code on node creation and node update of a certain content type in Drupal 7.
My problem now is that I can not call the function FB.api from withing this modules js files. Does it have something to do with Javascript namespaces? Running the FB.api() function from console works fine...
Thanks in advance for any help.
Nils
Based on your comments... I'm just watching your testModule.behaviors.js, and it doesn't even look similar to the function that I wrote to you here in the other topic.
Your current code is only like this:
FB.api(
'/me/shareatear:share',
'post',
{ tear: document.URL },
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('done. ' + response.id);
}
});
Where is Drupal.behaviors, and its attach function I showed you earlier? Where are all the other stuffs? :)
It's not even surprising that the current code outputs an error, because I think this JavaScript file is included before even defining the FB object, and this way you call this code directly in the header.
I think your testModule.behaviors.js file should look like this (based on the previous code we were talking about):
(function ($) {
Drupal.behaviors.testModule = {
doitnow: function () {
alert("A new "tear" content has just been added!");
// change this code to the appropriate one
FB.api('/me/shareatear:share', 'post', {
tear: document.URL
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('done. ' + response.id);
}
});
},
attach: function (context, settings) {
try {
if (settings.testModule.tear_just_added) {
this.doitnow();
}
} catch (ex) {}
}
};
})(jQuery);
So REPLACE your CURRENT content (where you are only calling FB.api without any Drupal-specific behaviors "wrappers"), and change its content to this one.
EDIT:
OK, try to set the weight of this module higher ONCE with the following db_query(), so its hooks get invoked later than the other modules' hooks. After putting these lines in your code, save the file, delete the Drupal cache, and after that, comment out the appropriate line! It's not needed to run all the time, in every page loads!
/**
* Implements hook_init()
* @see http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_init/7
*/
function testModule_init() {
// after putting this in your file, save it, delete cache, then COMMENT OUT THE FOLLOWING LINE!!! It should only RUN ONCE (it's enough).
db_query("UPDATE {system} SET weight = 111 WHERE type = 'module' AND name = 'testModule'");
}
这篇关于从 Drupal 7 中的模块 .js 文件调用全局 JS 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Drupal 7 中的模块 .js 文件调用全局 JS 函数
基础教程推荐
- npm start 错误与 create-react-app 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
