• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Function to add scripts in head in frontend mode
PostPosted: Tue Nov 13, 2018 9:44 am 
Offline
New Member
New Member

Joined: Thu Sep 30, 2010 3:23 pm
Posts: 6
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! :)


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Function to add scripts in head in frontend mode
PostPosted: Tue Nov 13, 2018 6:30 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Fri Dec 09, 2005 12:36 pm
Posts: 228
Location: Marlow, UK
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:
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__ )


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: [SOLVED] Function to add scripts in head in frontend mode
PostPosted: Thu Nov 15, 2018 3:50 pm 
Offline
New Member
New Member

Joined: Thu Sep 30, 2010 3:23 pm
Posts: 6
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:
 $this->AddEventHandler('Core', 'ContentPostRender', false);


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


Then, i had to uninstall and install again to see it work!


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Function to add scripts in head in frontend mode
PostPosted: Thu Nov 15, 2018 5:27 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 7966
Location: Fernie British Columbia, Canada
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:
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:
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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
A2 Hosting