Howdy,

This is an awesome module! Thanks to this module, I have successfully improved page load times. Thank you for creating it!

I have a couple of pages where I need to include some JS both in the head and in the body sections of the page, and for this pages, I need the JS to stay in its original location.

In order to achieve the above, is there any way to magically disable "Optimize JavaScript Ordering" and "Adjust javascript location and execution" for specific pages (say, for example, by path)?

Please advise.

Jorge

Comments

jorgemare created an issue. See original summary.

mikeytown2’s picture

Custom code would be the way to do exactly what you're describing. My preference is to use the arg() function for finding the current Drupal path.

/**
 * Implements hook_page_build().
 */
function jorgemare_page_build(&$page) {
  $arg = arg();
  // Disable js in footer and JavaScript Ordering if on the node/20 and node/20/* pages
  if ($arg[0] === 'node' && isset($arg[1]) && $arg[1] == 20) {
    $GLOBALS['conf']['advagg_mod_js_footer'] = 0;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_external'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_inline'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_browsers'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_head_extract'] = FALSE;
  }
}

Another alternative is to implement hook_advagg_mod_get_lists_alter() if it's a specific file that you want to target.

// $header_file_list: Do not move to footer file list.
// $header_inline_list: Do not move to footer inline list.
// $all_in_footer_list: Do not move this list of files/inline to the footer if this file is included.
hook_advagg_mod_get_lists_alter(&$header_file_list, &$header_inline_list, $no_async_defer_list, $inline_wrapper_list, $inline_js_wrap_skip_list, $inline_js_defer_skip_list, &$all_in_footer_list) {
}

Examples of this hook
AdvAgg will already not move some JS to the footer if `misc/ajax.js` is being used. This code looks like this

    // If there is a fast clicker, ajax links might not work if ajax.js is
    // loaded in the footer.
    $all_in_footer_list = array(
      'misc/ajax.js' => array(
        '/jquery.js',
        '/jquery.min.js',
        '/jquery.once.js',
        '/ajax.js',
        '/drupal.js',
        'settings',
      ),
    );

AdvAgg will also not move js to the footer for these files

    // Do not move to footer file list.
    $header_file_list = array(
      // Modernizr js.
      '/modernizr.',
      // Html5shiv and html5shiv-printshiv.
      '/html5shiv.',
      '/html5shiv-printshiv.',
    );

And if you have the google_admanager module installed it will not move the following inline code to the footer as well

      // Needs to be in the header.
      $header_inline_list[] = 'GS_googleAddAdSenseService(';
      $header_inline_list[] = 'GS_googleEnableAllServices(';
      $header_inline_list[] = 'GA_googleAddSlot(';
      $header_inline_list[] = 'GA_googleFetchAds(';
ilfelice’s picture

Hi mikeytown2!

Thank you so much for the prompt reply.

Not being much of a programmer, I have to ask: the arg() function sample code above, would that go in the theme's template.php file, or is it supposed to go into a custom module?

Jorge

ilfelice’s picture

To be more specific, assuming the code goes into template.php, would something like this work?

function theme-name_page_build(&$page) {
  $arg = arg();
  // Disable js in footer and JavaScript Ordering if on the inquiry/* pages
  if ($arg[0] === 'inquiry' && isset($arg[1])) {
    $GLOBALS['conf']['advagg_mod_js_footer'] = 0;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_external'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_inline'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_browsers'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_head_extract'] = FALSE;
  }
}
?>

Jorge

mikeytown2’s picture

template.php with the theme_name as hook will work.

I don't know if the path inquiry is an alias or an internal drupal path so I've covered both cases below.

/**
 * Implements hook_page_build().
 */
function themename_page_build(&$page) {
  $arg = arg();
  // Disable js in footer and JavaScript Ordering if on the inquiry/* pages
  if ($arg[0] === 'inquiry' && isset($arg[1]) || (strpos(drupal_get_path_alias(), 'inquiry/') === 0)) {
    $GLOBALS['conf']['advagg_mod_js_footer'] = 0;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_external'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_inline'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_browsers'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_head_extract'] = FALSE;
  }
}
mikeytown2’s picture

Status: Active » Fixed

Assuming #5 did the trick. Marking this issue as fixed.

ilfelice’s picture

Hello mikeytown2,

Apologies for the delay in responding. ;)

I added the above code in #5 to my theme template, making sure that I changed
portion in the name of the function to the theme name.

I then did a "drush cc" to flush all caches, and restarted apache to clear opcache.

After all this, I loaded a couple of pages under the inquiry path.

Unfortunately, the JS reordering and optimization was still happening.

The paths are all aliases, such as those below:

https://demo.com/inquiry
https://demo.com/inquiry/general
https://demo.com/inquiry/electronics
https://demo.com/inquiry/heatsink
https://demo.com/inquiry/medical
https://demo.com/inquiry/construction

I wonder if there is anything else that I need to do... Any further suggestions?

Jorge

mikeytown2’s picture

Status: Fixed » Active

Need to see if the hook is working and that the correct page is targeted. This will display a message.

/**
 * Implements hook_page_build().
 */
function themename_page_build(&$page) {
  drupal_set_message(__FUNCTION__ . ' called');
  $arg = arg();
  // Disable js in footer and JavaScript Ordering if on the inquiry/* pages
  if ($arg[0] === 'inquiry' && isset($arg[1]) || (strpos(drupal_get_path_alias(), 'inquiry/') === 0)) {
    drupal_set_message(__FUNCTION__ . ' correct path');
    $GLOBALS['conf']['advagg_mod_js_footer'] = 0;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_external'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_inline'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_browsers'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_head_extract'] = FALSE;
  }
}
ilfelice’s picture

mikeytown2,

I tried this code in #8 on a separate box, and no message was displayed.

Just in case, i switched themes (to Bartik), of course adding the code to template.php, but to no avail.

I must be doing something wrong, but just can't figure out what it is is...

Any suggestions?

mikeytown2’s picture

Instead of hook_page_build try hook_page_alter

ilfelice’s picture

mikeytown2,

> Instead of hook_page_build try hook_page_alter

Yay! That did the trick!

I did have to remove the backslash after inquiry in the if line, so that the exclusion would happen in the /inquiry path as well.

So, here is the code I used (in case it can be useful to others):

function global_page_alter(&$page) {
  $arg = arg();
  if ($arg[0] === 'inquiry' && isset($arg[1]) || (strpos(drupal_get_path_alias(), 'inquiry') === 0)) {
    $GLOBALS['conf']['advagg_mod_js_footer'] = 0;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_external'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_inline'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_adjust_sort_browsers'] = FALSE;
    $GLOBALS['conf']['advagg_mod_js_head_extract'] = FALSE;
  }
}

Thank again mikeytown2!

Jorge

mikeytown2’s picture

Status: Active » Fixed

Awesome, glad this worked out for you!

Status: Fixed » Closed (fixed)

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