Function to add scripts in head in frontend mode

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Locked
nicks
New Member
New Member
Posts: 6
Joined: Thu Sep 30, 2010 3:23 pm

Function to add scripts in head in frontend mode

Post by nicks »

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! :)
scooper
Forum Members
Forum Members
Posts: 239
Joined: Fri Dec 09, 2005 12:36 pm
Location: Marlow, UK

Re: Function to add scripts in head in frontend mode

Post by scooper »

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:

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;
			   }


	   }
	}
( you'll need to remove the extra underscores that the forum is adding before __html and __script__ )
nicks
New Member
New Member
Posts: 6
Joined: Thu Sep 30, 2010 3:23 pm

[SOLVED] Function to add scripts in head in frontend mode

Post by nicks »

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" :

Code: Select all

 $this->AddEventHandler('Core', 'ContentPostRender', false);
And this in "method.uninstall.php" :

Code: Select all

$this->RemoveEventHandler('Core', 'ContentPostRender');
Then, i had to uninstall and install again to see it work!
calguy1000
Support Guru
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

Post by calguy1000 »

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.

Code: Select all

function InitializeFrontend() {
    \CMSMS\HookManager::add_hook('Core::ContentPostRender',
     [this,'PostRenderHook'] );
}
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.

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.
Locked

Return to “Developers Discussion”