In custom module, run JavaScript function on page load

266 views
Skip to first unread message

BLee

unread,
Jun 15, 2016, 3:59:29 PM6/15/16
to Joomla! General Development
I'm brand new to Joomla module development, started yesterday just before noon. I'm trying to write a custom module that uses a Javascript plugin. It's not working and I've tracked it down to the init() call is not being fired for the plugin.

I'm trying to understand how to handle JavaScript in a Joomla module. Basically how to call a JS function (in a module) when the page loads. I've whittled my module down to:

===============================
<?php

// No direct access
defined('_JEXEC') or die;

$document = JFactory::getDocument();
$document->addScriptDeclaration('
window.event("domready", function() {
alert("An inline JavaScript Declaration");
});
');

==============================

Still nothing. So am I not getting the 'domready' event in my module? Is there another event that I should listen for to indicate my module is ready to use? Is there another technique I should be using?

I've been scouring the net for hours trying to figure out what I'm doing wrong.

seraphim

unread,
Jun 15, 2016, 6:19:41 PM6/15/16
to joomla-de...@googlegroups.com
Your code gives me: TypeError: window.event is not a function

Use jQuery instead it is easier to use:

$document->addScriptDeclaration('
jQuery("document").ready( function() {
alert("An inline JavaScript Declaration");
});
');



Op 15-06-16 om 21:18 schreef BLee:

Troy Hall

unread,
Jun 15, 2016, 6:39:44 PM6/15/16
to joomla-de...@googlegroups.com
window.event is a proprietary Microsoft Internet Explorer property which is only available while a DOM event handler is being called. Its value is the Event object currently being handled.
Bear

Sergio Manzi

unread,
Jun 15, 2016, 6:48:37 PM6/15/16
to joomla-de...@googlegroups.com
No need for the double quotes around "document":

$document->addScriptDeclaration('
jQuery(document).ready( function() {
alert("An inline JavaScript Declaration");
});
');


ghazal

unread,
Jun 16, 2016, 5:15:58 AM6/16/16
to Joomla! General Development
This below should be removed from the docs, it's a Mootools Javascript framework declaration.
Joomla gave up Mootools a couple of years ago, front-end wise.


==============================

Elvis

unread,
Jun 16, 2016, 8:14:20 AM6/16/16
to Joomla! General Development
If for some reason you want to use vanilla javascript without jquery, you do it like this

$document = JFactory::getDocument(); 
$document->addScriptDeclaration(')
  window.onload = function() {

Sergio Manzi

unread,
Jun 16, 2016, 8:29:22 AM6/16/16
to joomla-de...@googlegroups.com

I think in Mootools it should be

window.addEvent('domready', function() {...})

Mootols are still alive and kicking, unhappily...

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

piotr_cz

unread,
Jun 16, 2016, 9:35:50 AM6/16/16
to Joomla! General Development
Load event in jQuery binds to window, not document, see see https://api.jquery.com/load-event/

jQuery(window).on('load', function() {
    console
.log('hey');
});



On Thursday, June 16, 2016 at 2:29:22 PM UTC+2, Sergio Manzi wrote:

I think in Mootools it should be

window.addEvent('domready', function() {...})

Mootols are still alive and kicking, unhappily...


On 2016-06-16 11:15, ghazal wrote:
This below should be removed from the docs, it's a Mootools Javascript framework declaration.
Joomla gave up Mootools a couple of years ago, front-end wise.


==============================
=
<?php

// No direct access
defined('_JEXEC') or die;

$document = JFactory::getDocument();
$document->addScriptDeclaration('
    window.event("domready", function() {
              alert("An inline JavaScript Declaration");
    });
');

==============================
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Sergio Manzi

unread,
Jun 16, 2016, 9:43:34 AM6/16/16
to joomla-de...@googlegroups.com

.load is deprecated (since JQuery 1.8).

With JQuery, IF you want to run a function when the DOM is ready, the easiest, simplest and more straightforward way is:


jQuery(function() {
    console
.log('hey');
});
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

piotr_cz

unread,
Jun 16, 2016, 10:00:21 AM6/16/16
to Joomla! General Development
Yes .load is deprecated in favor of .on('load'

load event is different to domready in that it is triggered when all child assets are loaded (scripts, styles, images, iframes)

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsubscribe@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages