Current implementation of including js file from hook_init is too bad for site performance:

http://api.drupal.org/api/drupal/modules!system!system.api.php/function/...
To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

Please move this include to info file.

Comments

murz’s picture

For solve this we can add into .info file:
scripts[] = colorbox_node.js
scripts[] = /misc/ajax.js
and remove hook_init from module.

iLLin’s picture

Well hook_init will still be present to accommodate the global for webforms. I will move the js though to the info file.

murz’s picture

Existing hook_init (even if it do nothing) will break caching system for anonymous visitors, so much better is remove this function totally and move checking for global variable to other place.

As I think, you can remove line
$GLOBALS['colorbox_node_webform'] = FALSE;
too from this function, because in check line you have:
if (isset($GLOBALS['colorbox_node_webform']) && $GLOBALS['colorbox_node_webform']) {
so, if $GLOBALS['colorbox_node_webform'] isn't exist, first part will return false and all will work well.

iLLin’s picture

Good point, I have just always been a fan of initializing the variables even if they are not used. Should be available in the next DEV, then I will do a new release as I have quite a few fixes in.

iLLin’s picture

Well, it doesn't seem to let me add the /misc/ajax.js file to my info file like that. Still maybe out of luck, but I agree on the caching part so any other ideas?

murz’s picture

Did you clear the cache or reinstall module after changing info file?
On my site line
scripts[] = /misc/ajax.js
works well.

iLLin’s picture

How about this?

/*
 * Implments hook_preprocess_html().
 */
function colorbox_node_preprocess_html() {
  // We need to include our misc/ajax.js function all the time in case
  // we are loading up a webform in a colorbox window.
  drupal_add_library('system', 'drupal.ajax');
}

That seemed to work.

iLLin’s picture

My site this shows up for js. Maybe your ajax is included from another module and that's why it works? Verify your source.

<script type="text/javascript" src="http://drupal.projects.local/sites/all/modules/custom/colorbox_node//misc/ajax.js?m8swbm"></script>
iLLin’s picture

Committed to DEV

iLLin’s picture

Status: Active » Fixed
murz’s picture

Yes, ajax is loaded by other module, string
scripts[] = /misc/ajax.js
didn't solve the problem. So hook_preprocess_html is much better that hook_init, thanks for quick fixing this issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.