addHeaderItem inside Simple PHP Block

Permalink
I looked for a similar problem but they seem all related to controllers, templates and blocks.

I'm using that wonderful Simple PHP block and I'm trying to add some javascript that I can call from an .xsl file I'm importing, but the .xsl file apparently will only call javascript in the header, but the addHeaderItem doesn't seem to work within the Simple PHP Block. I tried it with a very simple example provided by someone else and no luck: (although it does work if I create my own block)

$this->addHeaderItem('<script type="text/javascript">alert("Up in da header!")</script>');

With the above line in a Simple PHP Block the script item does not appear anywhere in the source of the page.

Any ideas?

Thanks
-Steve

sday
 
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Hi Steve,

Don't quote me on this... I haven't used the Simple PHP block myself... but as I understand it, the "addHeaderItem" function has to be called from a block's controller because of WHEN it gets called. Putting it inside a page, it's too late. The header has already been generated... and the code engine that's rendering the page content doesn't have access to it. (or something like that).

That being said... does the JS HAVE to be in the header? If it's needed by a block on the page, I would think it could be added inline on the page as long as it's before the block that needs it?

- John
JohntheFish replied on at Permalink Reply
JohntheFish
For javascript, you can get round it by writing the <script> tag to the header using JavaScript (lazy load). There is a C5 javascript fn that does this hidden away in the dashboard somewhere, so you could copy that (or call it if it is already loaded).

Another way would be to take the load jquery.ui addon and change it to load the scripts you need in the attribute/event handler (or the block).
ScottC replied on at Permalink Reply
ScottC
Yeah these are snuck in here and there to set up some of the asset library click handlers or attach them to a jquery wrapped set of elements.
sday replied on at Permalink Reply
sday
Thanks guys. I was preparing to show you that it just doesn't work unless it goes in the header... and I wasn't sure why?

So I prepared this:

echo '<SCRIPT language="JavaScript">function anchor_test() { window.alert("This is an anchor test.") }</SCRIPT>';
echo '<A HREF="javascript:anchor_test()">Test</a>';

And it worked perfectly. LOL so I went back and saw that my <script tag was improperly created which prevented it from working... so I was in a steep downward spiral chasing ghosts.

it works now without the need for the addHeaderItem. And you added food for thought that it is too late to inject to the header and I'll have to explore the Dashboard ideas.

Thanks again,
-Steve