How do you render a piece of javascript code on every page? For example after I extended the view with the theme that I am using do I place the <script> tags in page/elements/header.php because the header is on every page?
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by RaĆ¼l Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.
- ihayredinov@ihayredinov
ihayredinov - 0 likes
- Olajide Ogundipe Jr@OllieGundi
Olajide Ogundipe Jr - 0 likes
- Burgs@burgs
Burgs - 0 likes
- ihayredinov@ihayredinov
ihayredinov - 0 likes
- Bennett@deepakr
Bennett - 0 likes
- ihayredinov@ihayredinov
ihayredinov - 0 likes
- ihayredinov@ihayredinov
ihayredinov - 0 likes
- Burgs@burgs
Burgs - 0 likes
You must log in to post replies.elgg_register_js() and elgg_load_js() in your init function will do.
Thanks, I couldnt ask for a better answer
@Olaj... For all elgg & PHP newbies could you please explain what file was "your init function" in and what was the the exact code you entered, including a few of the original lines from above and below your new code. It would be really helpful if you could. Thanks.
@Burgs,
Init function is the function that initializes your plugin. It should be contained in your plugin's start.php file and must be triggered by an event handler:
just place the code in default.php it will render the javascript in all the pages
Here is some useful info:
http://community.elgg.org/pg/forum/topic/700014/elgg-blog-elgg-18-including-css-and-javascript/
@Bennet, do not give wrong suggestions. That's not how it is supposed done in Elgg.
1. You do not just place the code in core files (http://docs.elgg.org/wiki/Plugin_development). It is considered bad practice, as you will loose track of what changes you have made, and once you upgrade you will loose all those. Save yourself time by doing things right.
2. There is reasoning behind elgg_register_js. JS files slow down page loading speed. elgg_load_js gives you control over where the JS is rendered and where not. There are only 2 JS files that must exist on all pages (jquery and jqueryUI). I doubt that there is any other JS file that needs to be included in 100% of pages. With elgg_load_js you can define criteria and load js dynamically once the criteria is met.
@Isma... Thanks
@All Every new member should read this topic