Hello,
I'm trying to write a module, and i need to add js script in the <head> section (though, it would even better if i could add it just before the closing <__body> tag but i fear it might be even harder, right?).
There is this nice function that does it in backend : GetHeaderHTML(), but i can't find a way to do it in frontend?
I see that Gallery module does it, but i can't figure out how. I've also searched in the API guide but can't find a function for it?
Thank you!
Function to add scripts in head in frontend mode
Re: Function to add scripts in head in frontend mode
You'll want to use the contentPostRender event.
In your main module class you can overload the DoEvent function, and if you then detect ContentPostRender you can add whatever extra code you need to.
The code below isn't tested but should insert a string before the closing /html tag:
( you'll need to remove the extra underscores that the forum is adding before __html and __script__ )
In your main module class you can overload the DoEvent function, and if you then detect ContentPostRender you can add whatever extra code you need to.
The code below isn't tested but should insert a string before the closing /html tag:
Code: Select all
function DoEvent( $originator, $eventname, &$params )
{
if ($originator == 'Core' && $eventname == 'ContentPostRender')
{
$tempcontent=$params['content'];
$pos=stripos($tempcontent,"</__html");
$code_to_add='<__script__>alert("boop");</__script>';
if($pos !== FALSE){
$tempcontent=substr($tempcontent,0,$pos).$code_to_add.substr($tempcontent,$pos);
$params['content'] = $tempcontent;
}
}
}
[SOLVED] Function to add scripts in head in frontend mode
Thank you scooper, your solution works like a charm.
In addition to create the function in the module class file, i just had to add this in "method.install.php" :
And this in "method.uninstall.php" :
Then, i had to uninstall and install again to see it work!
In addition to create the function in the module class file, i just had to add this in "method.install.php" :
Code: Select all
$this->AddEventHandler('Core', 'ContentPostRender', false);
Code: Select all
$this->RemoveEventHandler('Core', 'ContentPostRender');
-
- Support Guru
- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
- Location: Fernie British Columbia, Canada
Re: Function to add scripts in head in frontend mode
Just a Pro tip or two
a: You could use a hook (2.2+) so that you don't have to add and remove an event handler.
b: If your module is a plugin module. i.e: called by {MyModule} or {cms_module module=MyModule} then in it's action(s). I would set a member variable, that your event handler can use to conditionally insert the script tags.
a: You could use a hook (2.2+) so that you don't have to add and remove an event handler.
Code: Select all
function InitializeFrontend() {
\CMSMS\HookManager::add_hook('Core::ContentPostRender',
[this,'PostRenderHook'] );
}
Code: Select all
public method PostRenderHook($params) {
if( $this->myActionCalled ) {
// content is in $params['content']
// add my <__script__> tag in there.
}
return $params
}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.