Joomla - how to Unset JS file from head using info given by user in template parameters

398 views Asked by At

I've successfully unset both JS and CSS files manually from a Joomla template HTML head using the usual method below.

unset($this->_scripts['/media/jui/js/bootstrap.min.js']);
unset($this->_stylesheets['/media/jui/js/bootstrap.min.css']);

My question is this. Is it possible to store the file and path info into a variable and then use a variable in place of the path/file in the above examples? And if so, what might be a good way to do this?

The Reason? I want to give users of my template the ability to enter multiple files/paths separated by a comma into the template parameters, store these various files/paths into an array, then loop through that array (unsetting/removing each one from the HTML head of the template).

The purpose? Sometimes one needs to remove js from the html head and to the bottom of the page to reduce page load times, page transition flickers/white blinking, etc. In this case I don't want to use a plugin. I'd rather just give my users some fine grained control if possible built into the template. Of course, i'll have a place in the template parameters for the user to add whatever js they remove from the header to the bottom.

The code I'm using?

// grab the js file paths user enters into template parameters and add them to an array i can loop through

$unsetJSFiles = $this->params->get( 'customFilesUnsetJS' );
$unsetJSFilesArray = explode(',', $unsetJSFiles);

// loop through each js file path user entered and unset each one from joomla html head  

foreach ($unsetJSFilesArray as $valueJS) {
    $doc = JFactory::getDocument();
    unset($doc->_scripts[$valueJS]);
    }

It doesn't seem to be working. I have successfully tested that the values outputted what I'm looking for via print_r, echo, etc. But looping through doesn't work. This is the first real question I've asked on stackoverflow. Researched a lot of threads and googled, but this one i couldn't find an answer to. Many thanks for any advice!

1

There are 1 answers

0
AudioBubble On

Try to declare $doc outside the loop (above the loop)!

And you should unset it with THIS and declare the root!

unset($this->_scripts[JURI::root(true) . '/media/jui/js/jquery.min.js']);

So try (not tested!!):

// grab the js file paths user enters into template parameters and add them to an array i can loop through

$doc = JFactory::getDocument();
$unsetJSFiles = $this->params->get( 'customFilesUnsetJS' );
$unsetJSFilesArray = explode(',', $unsetJSFiles);

// loop through each js file path user entered and unset each one from joomla html head  

foreach ($unsetJSFilesArray as $valueJS) {        
    unset($this->_scripts[JURI::root(true) . $valueJS]);
}