Currently i am doing some page speed improvements to my site, and wanted to move all my javascripts to the page bottom.
My first approach was simply moving the script helper to the page bottom:
<?php include_javascripts() ?> </body>
But that wont worked as expected.
Some Widget or Subsites may write some inline scripts, that uses included class from the top of the page.
For example (original):
<script type="text/javascript" src="jquery.js" > <script type="text/javascript"> $.somefunction(); </script>
would result in (moved helper to the bottom):
<script type="text/javascript"> $.somefunction(); </script> <script type="text/javascript" src="jquery.js" ></script>
that leads to nasty javascript errors!
Here is the solution for that:
Its a simple symfony Filter which greps all script tags and moves them in correct order the bottom of the page!
activate the filter after your rendering filter in your filters.yml
rendering: ~ scripts: class: DomScriptFilter
Here is the Filter:
/**
* this filter moves all script tags to the bottom of the dom
*
* @author robert
* @package lib.filter
*/
class DomScriptFilter extends sfFilter
{
/**
* @var EXPRESSION finds all script tags and moves them to the bottom
*/
const EXPRESSION = "/<script [^>]*>([\d\D\w\W\s\S]*?)<\/script>/";
/**
* executes this filter
*
* @param sfFilterChain $filterChain
*/
public function execute($filterChain)
{
$filterChain->execute();
$content = $this->getContext()->getResponse()->getContent();
preg_match_all(self::EXPRESSION, $content, $scripts);
preg_replace(self::EXPRESSION, '', $content);
$content = str_replace('
Try to filter this. It’s valid html + javascript :twisted:
Testcase http://pastebin.com/ym9AXihT
There is no regex that can parse html in its complex. Not even approximately.
thanks for your solution.
i moved include_javascripts() to the end of the page and added the filter . but it seems that filter doesn’t work at all . because it doesn’t move other inline scripts to the bottom of the page.