IMPROVEMENT/FEATURE Forcefully write javascripts to the end of the HTML if wanted.

It's defaulted to false. But when set to true, the JS is written to the end of the HTML, even though there are earlier scripts.
This results in faster page-loading if the JS isn't needed earlier-on.
This commit is contained in:
Firesphere 2013-11-25 20:49:35 +01:00
parent ac1220758e
commit 14447b167a

View File

@ -308,6 +308,17 @@ class Requirements {
self::backend()->set_write_js_to_body($var);
}
/**
* Set the javascript to be forced to end of the HTML, or use the default.
* Useful if you use inline <script> tags, that don't need the javascripts
* included via Requirements::require();
*
* @param boolean $var If true, force the javascripts to be included at the bottom.
*/
public static function set_force_js_to_bottom($var) {
self::backend()->force_js_to_bottom = $var;
}
public static function debug() {
return self::backend()->debug();
}
@ -436,7 +447,15 @@ class Requirements_Backend {
* @var boolean
*/
public $write_js_to_body = true;
/**
* Force the javascripts to the bottom of the page, even if there's a
* <script> tag in the body already
*
* @var boolean
*/
public $force_js_to_bottom = false;
public function set_combined_files_enabled($enable) {
$this->combined_files_enabled = (bool) $enable;
}
@ -711,7 +730,7 @@ class Requirements_Backend {
$p2 = stripos($content, '<body');
$p1 = stripos($content, '<script', $p2);
if($p1 !== false) {
if($p1 !== false && !$this->force_js_to_bottom) {
$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
} else {
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
@ -1152,4 +1171,4 @@ class Requirements_Backend {
Debug::show($this->combine_files);
}
}
}