[英]How to call a javascript function from .js file in a Drupal module file
[英]Passing information to JS File from Module File in Drupal 7
在我的网页之一中,我多次打印一个按钮,并且每个按钮都有不同的logId。 我希望当用户单击时将JQuery插入图片,并且应该没有服务器端回发。 由于每个按钮都具有唯一的日志ID,因此单击按钮时,我将信息从* .Module传递到JS文件,如下所示:
* .module文件-PHP 文件
$thm_button .= '<input type="button" class="clsPrevious" id="btnPrev_'.$user->uid.'_'.$logid;>';
* .js文件
$('.clsPrevious', context).click(function (event) {
var previousLog=this.id.split('_');
//Retrieve log Id and Process the log id using AJAX call
}
这在这里工作正常,但我感到有安全隐患,因为每个人都可以在HTML源代码中查看按钮的日志ID。
Drupal / PHP / HTML中是否有任何方法可以将敏感信息传递给JS,而无需在HTML Viewer中显示。
您可以使用“ Drupal.settings”将值从PHP传递到Javascript。
在此处了解更多信息。
您可以创建一个看起来像这样的输入按钮。
$thm_button .= '<input type="button" class="clsPrevious" data-myModule=" . $key . ">;
其中$key
是随机/串行变量,但每个Log ID唯一。
创建键值对的映射数组,并使用drupal_add_js ()传递给javasript
<?php
drupal_add_js(array('myModule' => array('_YOUR_CUSTOM_KEY_1_' => '_LOG_ID_1_')), 'setting');
?>
现在将您的点击功能修改为
$('.clsPrevious', context).click(function (event) {
var key = $(this).attr("data-myModule");
// Now a this point you have the lookup table in Drupal.settings.myModule
// use the "key" variable to find the LOG ID
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.