Loading CTools modal on page load for specific pages

This is my first solution in series of solutions for automatic loading of modal forms. If you have a link on some page that you want to load on page load. You need to first create JS that would make that and then find a way to add that JS to specific pages. So first JS

(function($){
  Drupal.ajax.prototype.specifiedResponse = function() {
    var ajax = this;

    if (ajax.ajaxing) {
      return false;
    }
    try {
      $.ajax(ajax.options);
    }
    catch (err) {
      alert('An error occurred while attempting to process ' + ajax.options.url);
      return false;
    }

    return false;
  };
 

  $(document).ready(function() {
    Drupal.ajax[$('a.ctools-use-modal').attr('href')].specifiedResponse();
  });
 
})(jQuery);    

This basically loads any link on page that has 'ctools-use-modal' class as modal. If you add this to module and add to module info file it will load on every page. So you need to limit that. You could limit that per content type e.g.

function mymodule_preprocess_page(&$vars) {

    if (isset($vars['node']) && $vars['node']->type == 'page') {
    drupal_add_js(drupal_get_path('module', 'king_custom') . '/js/king_custom.js');
    $vars['scripts'] = drupal_get_js();
  }
}

And it will run only on page content type nodes. Or if you want to run on on specific node. Just add that

    drupal_add_js(drupal_get_path('module', 'my_module') . '/js/custom.js');to the node with php filter and it will be loaded only on that page. Maybe a bit cleaner version would be to add this to your module and init it only on specific pages.

if ($_GET['q'] == 'your_page_name') {
   drupal_add_js(drupal_get_path('module', 'your_module') .'/your_module.js');
}

Challenge for me is how to do all of this if there is no link on the page. Hopefully I will post about this in next blog post.