Send and Check if the variable exist in JS sent on php level

Last updated on 13/09/2015

After sending the variable from PHP file to js file in Drupal 7:

drupal_add_js(array('my_module' => array('inactive_terms' => $testvar)), array('type' => 'setting'));

You need make proper checks for variable existence as this may break all existing JavaScript execution:

 
 Drupal.behaviors.example_behavior = {
    attach: function(context, settings) {
 
      // Check if the variable exist.
      var dSettings = Drupal.settings;
      if (dSettings.my_module != undefined) {         
        x = (dSettings.my_module.inactive_terms != undefined) ? dSettings.my_module.inactive_terms : ''; 
        if (x != '') {  
        // Variable exist.   
        }
      }
 
    }
  }