Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:16:13+00:00 2026-06-05T09:16:13+00:00

I would like to execute a JavaScript function only on node creation and node

  • 0

I would like to execute a JavaScript function only on node creation and node edit of one specific content type. It is only one small function call.

Let’s call the Drupal 7 content type “tear“, and the JavaScript function “doitnow();“

Any ideas how to do this?

Thank you very much.

Nils

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:16:15+00:00Added an answer on June 5, 2026 at 9:16 am

    You have to create your own module and implement hook_node_insert(), hook_node_update() and hook_node_view() and take the appropriate steps. You also have to create some JavaScript codes with Drupal behavior, and you’re ready. You have to use a temporary session variable after node creation/update, because this way you can check the existance of this variable in hook_node_view, and add the JS setting which “tells” a new “tear” content has been added.

    You can download the whole module here.

    You have to put this module to the sites/all/modules/testModule directory.

    OK, here’s the code of the module:

    testModule.info:

    name = Test Customization Module
    description = Customizing stuffs on the site...
    core = 7.x
    package = My modules
    
    scripts[] = js/testModule.behaviors.js
    

    testModule.module:

    /**
     * Implements hook_node_insert()
     * 
     * @see http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_insert/7
     * @param object $node 
     */
    function testModule_node_insert($node) {    
        switch ($node->type) {
            case 'tear':
                $_SESSION['tear_just_inserted'] = microtime();
                break;
        }
    }
    
    /**
     * Implements hook_node_update()
     * 
     * @see http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_update/7
     * @param object $node 
     */
    function testModule_node_update($node) {
        switch ($node->type) {
            case 'tear':
                $_SESSION['tear_just_inserted'] = microtime();
                break;
        }
    }
    
    /**
     * Implements hook_node_view().
     */
    function testModule_node_view($node, $view_mode, $langcode) {
        switch ($node->type) {
            case 'tear':
                // we call doitnow() if it's needed (if "tear" content was added before)
                if (isset($_SESSION['tear_just_inserted'])) {
                    drupal_add_js(array('testModule' => array('tear_just_added' => TRUE)), 'setting');
                    unset($_SESSION['tear_just_inserted']);
                }
    
                break;
        }
    }
    
    /**
     * Implements hook_overlay_child_initialize()
     * @see http://api.drupal.org/api/drupal/modules!overlay!overlay.api.php/function/hook_overlay_child_initialize/7
     */
    function testModule_overlay_child_initialize() {
        // Add our custom JavaScript: we don't want the node/add/tear to appear on the overlay, because this way the JS setting doesn't get added properly
        drupal_add_js(drupal_get_path('module', 'testModule') . '/js/testModule.overlay.behaviors.js');
    }
    

    js/testModule.behaviors.js:

    (function ($) {   
        Drupal.behaviors.testModule = {
            doitnow: function(){
                alert("A new \"tear\" content has just been added!");
            },
    
            attach: function (context, settings) {
                try{
                    if(settings.testModule.tear_just_added){
                        this.doitnow();
                    }
                } catch(ex){}
            }
        };
    })(jQuery);
    

    js/testModule.overlay.behaviors.js:

    (function ($) {
        // Disable overlay for creating the "tear" node.
        Drupal.behaviors.testModule = {
            attach: function (context, settings) {
                var $tear_link = $('a[href*="node/add/tear"]'),
                tear_link_href = $tear_link.attr('href');
                if( $tear_link.length > 0 ) {
                    // we want to avoid opening the content adding for "tear" content type in the overlay
                    // so we open it in the current window
                    $tear_link.unbind();
                    $tear_link.click(function(e){
                        window.open(tear_link_href,'_self',false);
                        e.stopPropagation();
                    })
                }    
            };
        }
    })(jQuery);
    

    Ask if something isn’t quite clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to execute a JavaScript function after the page was loaded. At
I have a javascript function that I would like to execute after every asynchronous
I have a specific DB2 query, and I would like to execute this query
i only know basic javascript and not even basic jquery, but i would like
I would like to execute a function when Simplemodal opens a window, and have
I am trying to execute JavaScript using Winforms & I would like to fetch
May be duplicate. I would like to write javascript which can execute linux command
I would like to execute a MySQL command in a shell script/cron job that
I would like to execute some kind of subquery with my fetchedresultscontroller. I've got
I would like to execute a procedure periodically, how to do that in MySQL?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.