I have written some JavaScript that I want to only run on the Edit page. We are using Drupal 7.8 and in our setup the edit page opens as an overlay. How do I get this javascript to run when the edit overlay page opens up and only then?

Thanks! Jon

  • "Node" Edit Page? – Beebee Jul 12 '13 at 13:42
  • Hmm, maybe. I could probably do it by adding a custom node edit tpl.php file. But I really just want to add a javascript file that will listen to an event and trigger with that event. What file name will be the node edit tpl.php file and will I need one for each content type? – Jon H Jul 12 '13 at 14:07
  • 1
    I asked if you meant "Node edit page" when you said "Edit page" in your question. You can have Edit pages for taxonomy terms, vocabularies, content types, various entities, users, configurations... which is it? Do you just want to run a javascript file on the node edit page? Please clarify. Thank you. – Beebee Jul 12 '13 at 14:23
  • @Beebee is right, we need to know what are you trying to edit. And what overlay? One from core? Asking to be sure. – Mołot Jul 12 '13 at 14:26
  • Oh. Sorry. Yes, the node edit page from core. I have not made any changes to the edit pages so far because they have all worked fine for our needs so far. – Jon H Jul 12 '13 at 14:28

Regardless of whether the page opens in an overlay or not, the following should work. Suppose you have a node type called "Basic Page", and the machine name of the content type is "page". The node edit forms take the form of: MACHINENAME_node_form. So for "page" content type our form id is: page_node_form. The hook we're using is hook_form_FORM_ID_alter. The following code placed in a custom module should attach the JS file (replace instances of word hook with the module name, and /path/to/file.js with the path to the javascript file relative to the custom module directory):

function hook_form_page_node_form_alter(&$form, &$form_state, $form_id) {
  // Attach a JS file
  $form['#attached']['js'][] = drupal_get_path('module', 'hook') . '/path/to/file.js';

For reference: Form API #attached

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.