Hello,
How do I add my Javascripts to an existing Elgg core page?
I tried to add them to views/default/js and load them with elgg_load_js() on pages, but the Elgg engine does not load them for me.
Many thanks!
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.
- Juho Jaakkola@juho.jaakkola

Juho Jaakkola - 0 likes
- Evan Winslow@ewinslow

Evan Winslow - 0 likes
- CCH@chioujack

CCH - 0 likes
- Juho Jaakkola@juho.jaakkola

Juho Jaakkola - 0 likes
- CCH@chioujack

CCH - 0 likes
- Juho Jaakkola@juho.jaakkola

Juho Jaakkola - 0 likes
- CCH@chioujack

CCH - 0 likes
- Juho Jaakkola@juho.jaakkola

Juho Jaakkola - 0 likes
- CCH@chioujack

CCH - 0 likes
You must log in to post replies.See: Administration > Settings > Advanced settings
And check whether the cache is enabled. It should be disabled during development.
1.8 or 1.9? What is the exact file path you're using? What are the contents of the script? Where are you calling elgg_load_js?
Hello,
I am using Elgg 1.8.19.
I always flush the cache after any change. Now, I disable all cache functions.
My Javascript is <Elgg root>/views/default/js/myjavascript.php.
The content of the script is
=====
elgg.provide('elgg.myjavascript');
elgg.myjavascript.init = function() { };
elgg.register_hook_handler('init', 'system', elgg.myjavascript.init);
$(document).ready(function() {
alert("hello world");
}
=====
I want to preprocess the data in the login form so I call elgg_load_js('elgg.myjavascript') in <Elgg root>/views/default/forms/login.php. However, when I load the login page, the window does not popup, and I do not see my script is loaded in any <script> block.
Thanks!
How to add the javascript without modifying core:
Create the file <Elgg root>/views/default/mod/<yourplugin>/views/default/include_js.php and add:
Then in <yourplugin>_init() call:
The file loading the javascript gets now added to all pages that use the login form.
Do those changes, and let's see if you still run into problems.
Do I have to follow the directions for creating a regular Elgg Plugin to create <yourplugin>?
In other words, do I have to create a complete plugin skeleton for <yourplugin> in <Elgg root>/views/default/mod/ ? Is <yourplugin>_init() called by <Elgg root>/views/default/mod/<yourplugin>/start.php?
Yes, modifications like this should always be done with a plugin. See: http://docs.elgg.org/wiki/Dont_Modify_Core
Yes.
Yes.
Here are tutorials that may help you: http://learn.elgg.org/en/1.x/tutorials/index.html
I have several my own regular Elgg Plugins.
Since this special <yourplugin> does nothing more than loading my Javascripts, I expect that the actions/, languages/, pages/, and manifest.xml are not required. Only start.php, and views/ are necessary.
In the start.php,
I only have to
(1) create <yourplugin>_init().
(2) register <yourplugin>_init() with elgg_register_event_handler().
(3) register my Javascripts with elgg_extend_view(), elgg_register_simplecache_view(), elgg_get_simplecache_url(), and elgg_register_js().
<yourplugin> is automatically activated and enhances the login form.
Is my understanding correct?
Many thanks!
Otherwise correct, but but manifext.xml is also required.
I still cannot see my Javascipts loaded as a js file in the source code of the login page.
The following is my Elgg 1.8.19 configuration.
<Elgg root>/views/default/js/specialplugin_javascript.php
<Elgg root>/views/default/mod/specialplugin/start.php
<Elgg root>/views/default/mod/specialplugin/manifest.xml
<Elgg root>/views/default/mod/specialplugin/views/default/include_js.php
The content of
<Elgg root>/views/default/js/specialplugin_javascript.php
is
=====
elgg.provide('elgg.specialplugin');
elgg.specialplugin.init = function() { };
elgg.register_hook_handler('init', 'system', elgg.specialplugin.init);
$(document).ready(function() {
alert("hello world");
});
=====
The content of
<Elgg root>/views/default/mod/specialplugin/start.php
is
=====
<?php
elgg_register_event_handler('init', 'system', 'specialplugin');
function specialplugin_init() {
elgg_extend_view('forms/login', 'specialplugin/views/default/include_js');
elgg_register_simplecache_view('js/specialplugin/specialplugin_javascript');
$url = elgg_get_simplecache_url('js', 'specialplugin/specialplugin_javascript');
elgg_register_js('specialplugin', $url);
}
?>
=====