从Drupal 7中的.js文件中调用全局JS函数

编程入门 行业动态 更新时间:2024-10-08 11:00:38
本文介绍了从Drupal 7中的.js文件中调用全局JS函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 Sk8erPeter的极大帮助我设法在Drupal 7中的某个内容类型的节点创建和节点更新上执行Javascript代码。

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.

我现在的问题是我无法调用函数FB。 api从这个模块js文件。它与Javascript命名空间有关吗?从控制台运行FB.api()函数工作正常...

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...

提前感谢任何帮助。

Nils

推荐答案

根据您的意见...我只是看着你的 testModule.behaviors.js ,它甚至与我写给你的功能看起来不一样另一个主题中的这个。

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); } });

Drupal.behaviors 在哪里附加函数我早点显示了你?所有其他的东西在哪里? :) 目前的代码输出错误并不奇怪,因为我认为这个JavaScript文件包含在 之前,甚至定义 FB 对象,这样就可以直接在标题中调用此代码。

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.

我认为你的 testModule .behaviors.js 文件应该是这样的(根据我们前面的代码):

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);

所以更换你的CURRENT内容(你只打电话给$ code> FB.api 没有任何 Drupal特定的行为 包装),并将其内容更改为此。

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.

编辑:

确定,尝试设置重量的这个模块更高的ONCE与以下 db_query(),所以它的钩子比其他模块的钩子晚于调用。将这些行放入代码后,保存文件,删除Drupal缓存,然后注释掉相应的行!不需要运行所有的时间,在每个页面加载!

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 api.drupal/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函数

本文发布于:2023-11-28 17:54:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   函数   文件   Drupal   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!