You are here

function advagg_mod_defer_inline_js in Advanced CSS/JS Aggregation 7.2

Callback for preg_replace_callback.

Used to wrap inline JS in a function in order to defer the inline js code.

Parameters

string $input: JavaScript code to wrap in setTimeout.

Return value

string Inline javascript code wrapped up in a loader.

2 calls to advagg_mod_defer_inline_js()
advagg_mod_ga_inline_to_file in advagg_mod/advagg_mod.module
Move analytics.js to be a file instead of inline.
advagg_mod_inline_defer in advagg_mod/advagg_mod.module
Defer inline js by using setTimeout.

File

advagg_mod/advagg_mod.module, line 2869
Advanced aggregation modifier module.

Code

function advagg_mod_defer_inline_js($input) {
  if (variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY) !== FALSE && variable_get('jquery_update_jquery_cdn', 'none') !== 'none' && (stripos($input, 'jQuery') !== FALSE || strpos($input, '$(') !== FALSE || stripos($input, 'Drupal.') !== FALSE)) {
    $matches[2] = $matches[0] = $input;
    return advagg_mod_wrap_inline_js($matches);
  }

  // Get inline defer js skip list.
  list(, , , , , $inline_js_defer_skip_list) = advagg_mod_get_lists();
  if (!empty($inline_js_defer_skip_list)) {

    // If the line is on the skip list then do not inline the script.
    foreach ($inline_js_defer_skip_list as $string_to_check) {
      if (stripos($input, $string_to_check) !== FALSE) {
        return $input;
      }
    }
  }

  // Use a counter in order to create unique function names.
  static $counter;
  ++$counter;

  // JS wrapper code.
  $new = "\nfunction advagg_mod_defer_{$counter}() {\n  {$input};\n}\nwindow.setTimeout(advagg_mod_defer_{$counter}, 0);";
  return $new;
}