Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Including js only if a function is called.
For example,
Let’s say I want javascript to be called into the head iff a specific plugin has been called to action. Is there a method for replacing what is in the head of the document?
And if so, how could I do this within the plugin code itself?
One idea that I had to accomplish this was to find n’ replace within the variable that pumps out the html of a specific page using the plugin. Is it possible? What variable would it be!
Proud Canadian. Toronto Locksmith , Actualize Consulting
Offline
Re: Including js only if a function is called.
Well, I’m not entirely sure how one would go about this, but one idea that strikes my mind would be to find and replace “/ head” (silly forum strips < & >) with the js and then close the head tag.
As far as implementing this though, I’m not really sure how you would do it. You might want to check out some of the admin plugins, which use DOM scripting.
I’m pretty sure it has something to do with the <code>register_callback()</code> function, but this is one that has eluded me thus far (although I haven’t really had any need for it, so…)
Last edited by akokskis (2005-11-14 03:26:50)
Offline
#3 2005-11-14 04:01:33
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: Including js only if a function is called.
Output buffering.
if (txpinterface == 'public')
{
ob_start('ext_yourplugin_load_js');
}
function ext_yourplugin_load_js($buffer)
{
$find = '</head>';
$replace = n.'<script type="text/javascript" src="script-url.js"></script>'.n.$find;
return str_replace($find, $replace, $buffer);
}
Offline
Re: Including js only if a function is called.
Ma ma ma Mary… I need some loving… ( album scratches ).
Thanks Mary!
/me runs to go try it now.
Proud Canadian. Toronto Locksmith , Actualize Consulting
Offline