mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
ac1220758e
commit
14447b167a
@ -308,6 +308,17 @@ class Requirements {
|
|||||||
self::backend()->set_write_js_to_body($var);
|
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() {
|
public static function debug() {
|
||||||
return self::backend()->debug();
|
return self::backend()->debug();
|
||||||
}
|
}
|
||||||
@ -437,6 +448,14 @@ class Requirements_Backend {
|
|||||||
*/
|
*/
|
||||||
public $write_js_to_body = true;
|
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) {
|
public function set_combined_files_enabled($enable) {
|
||||||
$this->combined_files_enabled = (bool) $enable;
|
$this->combined_files_enabled = (bool) $enable;
|
||||||
}
|
}
|
||||||
@ -711,7 +730,7 @@ class Requirements_Backend {
|
|||||||
$p2 = stripos($content, '<body');
|
$p2 = stripos($content, '<body');
|
||||||
$p1 = stripos($content, '<script', $p2);
|
$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);
|
$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
|
||||||
} else {
|
} else {
|
||||||
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
||||||
|
Loading…
Reference in New Issue
Block a user