im drupal themer , im struggling modify module in way need. when happens need run simple bit of javascript. ive found part of module responsable , following works:
if (something = else) { return array( '#commands' => array( // hacky works ajax_command_append('body', '<script> alert("custom js"); </script>'), ), ); } else { // else } however better call function somewhere else other modules want call it. how can call function module within statement?
you can inline, this
edit
in first answer forgot wrap javascript in jquery(document).ready(function() {})
module
if (something = else) { // inline fixed drupal_add_js("jquery(document).ready(function() { alert('script inline!'); });", "type" => "inline"); // file drupal_add_js("myscript.js", "file"); return array( '#commands' => array( ), ); } else { // else } myscript.js
(function() { alert('script file!'); }); // or jquery (function($) { // $ })(jquery); if want javascript code in on own file, (taken examples)
check out examples in documentation
more information on adding javascript modules/themes
adding javascript theme or module
managing javascript in drupal 7
edit 2
you can call javascript function module. :
- create new module inject javascript code. module should have lesser weight other module executes before of them.
- then call module using
drupal.settings.
assuming created module, should save javascript function drupal.settings
(function ($) { drupal.settings.examplemodule = { myfunction : function () { alert('my example function!'); } }; }(jquery)); this inject code settings , available module needs access it.
your module
if (something = else) { // inline fixed drupal_add_js("(function($) { drupal.settings.examplemodule.myfunction(); })(jquery);", "type" => "inline"); } else { // else } that's how it. now, if need use ajax framework should (try it, i've never used framework before, i'm guessing)
return array( '#commands' => array( // hacky works ajax_command_append('body', "(function($) { drupal.settings.examplemodule.myfunction(); })(jquery);"), ), );
Comments
Post a Comment