Skip to main content
added 42 characters in body
Source Link
CocoSkin
  • 891
  • 14
  • 37

My idea:
I am trying to use a module to manage all the js and css with #attached method.
#attached method I found from:

My module folder containing:
testing.info
testing.module
js/mask.js

In testing.info, it's including file testing.module
files[] = testing.module

In testing.module, I'm trying to use the #attached method to attach "jquery mask" to all form tags, including add/edit node, webform field.
Here is my code but it does not work. -___-

function testing_init() {
  $build['#attached']['js']['https://cdn.testing.com/jquery.mask.min.js'] = array('type' => 'external');
  $form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';
}

Question:
Am I using the wrong method to attach js to form tag?
I don't want to load these files on all pages, what is the better way?

My idea:
I am trying to use a module to manage all the js and css with #attached method.
#attached method I found from:

My module folder containing:
testing.info
testing.module
js/mask.js

In testing.info, it's including file testing.module
files[] = testing.module

In testing.module, I'm trying to use the #attached method to attach "jquery mask" to all form tags, including add/edit node, webform field.
Here is my code but it does not work. -___-

$build['#attached']['js']['https://cdn.testing.com/jquery.mask.min.js'] = array('type' => 'external');
$form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';

Question:
Am I using the wrong method to attach js to form tag?
I don't want to load these files on all pages, what is the better way?

My idea:
I am trying to use a module to manage all the js and css with #attached method.
#attached method I found from:

My module folder containing:
testing.info
testing.module
js/mask.js

In testing.info, it's including file testing.module
files[] = testing.module

In testing.module, I'm trying to use the #attached method to attach "jquery mask" to all form tags, including add/edit node, webform field.
Here is my code but it does not work. -___-

function testing_init() {
  $build['#attached']['js']['https://cdn.testing.com/jquery.mask.min.js'] = array('type' => 'external');
  $form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';
}

Question:
Am I using the wrong method to attach js to form tag?
I don't want to load these files on all pages, what is the better way?

Source Link
CocoSkin
  • 891
  • 14
  • 37

Using #attached to attach js to form by a custom module

My idea:
I am trying to use a module to manage all the js and css with #attached method.
#attached method I found from:

My module folder containing:
testing.info
testing.module
js/mask.js

In testing.info, it's including file testing.module
files[] = testing.module

In testing.module, I'm trying to use the #attached method to attach "jquery mask" to all form tags, including add/edit node, webform field.
Here is my code but it does not work. -___-

$build['#attached']['js']['https://cdn.testing.com/jquery.mask.min.js'] = array('type' => 'external');
$form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';

Question:
Am I using the wrong method to attach js to form tag?
I don't want to load these files on all pages, what is the better way?