function advagg_mod_wrap_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 prevent js errors when JS is moved to the footer, or when js is loaded async.
Parameters
array $matches: $matches[0] is the full string; $matches[2] is just the JavaScript.
string $check_string: JavaScript if statement; when true, run the inline code.
int $ms_wait: Default amount of time to wait until the required code is available.
Return value
string Inline javascript code wrapped up in a loader to prevent errors.
6 calls to advagg_mod_wrap_inline_js()
- advagg_mod_advagg_modify_css_pre_render_alter in advagg_mod/
advagg_mod.module - Implements hook_advagg_modify_css_pre_render_alter().
- advagg_mod_defer_inline_js in advagg_mod/
advagg_mod.module - Callback for preg_replace_callback.
- advagg_mod_devel_query_put_contents in advagg_mod/
advagg_mod.module - Writes the variables information to a file.
- advagg_mod_inline_defer in advagg_mod/
advagg_mod.module - Defer inline js by using setTimeout.
- advagg_mod_js_async_defer in advagg_mod/
advagg_mod.module - Add the defer and or the async tag to js.
1 string reference to 'advagg_mod_wrap_inline_js'
- advagg_mod_js_inline_processor in advagg_mod/
advagg_mod.module - Given html, do some processing on the script tags included inside it.
File
- advagg_mod/
advagg_mod.module, line 2922 - Advanced aggregation modifier module.
Code
function advagg_mod_wrap_inline_js(array $matches, $check_string = NULL, $ms_wait = 250) {
list(, , , $inline_wrapper_list, $inline_js_wrap_skip_list) = advagg_mod_get_lists();
if (empty($check_string)) {
foreach ($inline_wrapper_list as $search_string => $js_condition) {
if (strpos($matches[2], $search_string) !== FALSE) {
$check_string = $js_condition;
break;
}
}
if (empty($check_string)) {
$check_string = 'window.jQuery && window.Drupal && window.Drupal.settings';
}
}
// Always wrap inline if it contains jquery or drupal.
if (!empty($inline_js_wrap_skip_list) && stripos($matches[2], '(jQuery') === FALSE && stripos($matches[2], 'jQuery.') === FALSE && stripos($matches[2], '(Drupal') === FALSE && stripos($matches[2], 'Drupal.') === FALSE) {
// If the line is on the skip list then do not inline the script.
foreach ($inline_js_wrap_skip_list as $string_to_check) {
if (stripos($matches[2], $string_to_check) !== FALSE) {
return $matches[0];
}
}
}
// Use a counter in order to create unique function names.
static $counter;
++$counter;
// JS wrapper code.
$new = "\nfunction advagg_mod_{$counter}() {\n // Count how many times this function is called.\n advagg_mod_{$counter}.count = ++advagg_mod_{$counter}.count || 1;\n try {\n if (advagg_mod_{$counter}.count <= 40) {\n {$matches[2]}\n\n // Set this to 100 so that this function only runs once.\n advagg_mod_{$counter}.count = 100;\n }\n }\n catch(e) {\n if (advagg_mod_{$counter}.count >= 40) {\n // Throw the exception if this still fails after running 40 times.\n throw e;\n }\n else {\n // Try again in {$ms_wait} ms.\n window.setTimeout(advagg_mod_{$counter}, {$ms_wait});\n }\n }\n}\nfunction advagg_mod_{$counter}_check() {\n if ({$check_string}) {\n advagg_mod_{$counter}();\n }\n else {\n window.setTimeout(advagg_mod_{$counter}_check, {$ms_wait});\n }\n}\nadvagg_mod_{$counter}_check();";
$return = str_replace($matches[2], $new, $matches[0]);
return $return;
}