add javascript to head from blocks view

Permalink 1 user found helpful
How can I add a javascript file from within a blocks view.php template? (and add it to the head of the page)

The reason I am trying is because it is a page_list template that uses jquery-ui. Normally, this javascript file would reside in /templates/my_page_list/js/ and then get loaded automatically by c5 ... but there is a conflict between the jquery-ui the block loads and the jquery-ui c5 loads when logged in to edit the site.

So I need to only load the javascript to the page the block is on when the user is not logged in to edit the site.

At first I thought I would do this through the block's controller.php file however I would like to avoid duplicating the entire controller file for just this one template. Hence, my using the template view file to add the javascript.

I've figured out how to add javascript from the block's view template but I can't seem to find any information on how to add it to the HEAD of the page. So far it's just including an inline script tag in the content of the page.

To load specific javascript from block view template only when NOT logged in as site editor:
//added in: packages/my_package/blocks/page_list/templates/my_page_list/view.php
   $htmlHelper = Loader::Helper('html');
   Global $c;
   $cp = new Permissions($c);
   if(!$cp->canAdmin() && !$cp->canAddSubContent()){
      echo($htmlHelper->javascript('http://path/to/package/blocks/page_list/templates/custom_page_list/jquery-ui-1.7.3.custom.min.js'));
   }

The end result works but it inserts the script tag in my body content rather than the head.

Anyone know how to add js to the head from within a block's template?

Thanks for your time and help and also I'd love to know if you have any tips or suggestions in order to resolve the javascript conflict when logged into the site as administrator / editor, so far it's working with the above code but it seems hackish.

MrNiceGaius
 
Mnkras replied on at Permalink Reply
Mnkras
there is no "neat" way to do this right now unfortunately
jelthure replied on at Permalink Reply
jelthure
is there any special reason you require different versions of the jquery.ui.js?
Mnkras replied on at Permalink Reply
Mnkras
Wanna test everything in c5 that uses jquery to make sure nothing breaks ;)
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
No. It came with the package but because it is named with 'custom' I figured something was different.

Actually, the default c5 jquery-ui file works fine. Is there a way to load that?
Mnkras replied on at Permalink Reply
Mnkras
just do $htmlHelper->javascript(jquery.ui.js)
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Thanks Mnkras,
echo($htmlHelper->javascript('jquery.ui.js'));

-pulls the default c5 jquery-ui

-displays inline script tag
-works front end and while logged in backend
TheRealSean replied on at Permalink Reply
TheRealSean
you can also load your javascript into a view.js this is automatically included in the page, similar to view.php and view.css
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Good point! Do you ever use your view.js to load additional scripts into the head section?
TheRealSean replied on at Permalink Reply
TheRealSean
I have used it a couple of times now.

Only recently though as it was something that I noticed a couple of sites back when trying to include extra js from the template.

I usually then just take whatever file I wanted to include and copy paste into the view.js just to save the hassle of loading in extra js from the head on a conditional if statement
tailorweb replied on at Permalink Reply
is there any specific reason you want to add script to the head?

you can place script anywhere on your page even inside <div>

google is even advocating people to put <script> at the end of the html, even after the </html> tag, to speed up loading of the website
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Good question! I add almost all of my scripts before the closing body tag... now Google says after the last html tag (cool! I didn't know that was "kosher" :)

Since the blocks controller.php file can insert custom scripts into the head of the page I starting thinking there was a way to do the same thing but from the view.php file instead...

Maybe I was just stuck on the idea of the code looking pretty? haha