In custom module, run JavaScript function on page load

263 wyświetlenia
Przejdź do pierwszej nieodczytanej wiadomości

BLee

nieprzeczytany,
15 cze 2016, 15:59:2915.06.2016
do 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

nieprzeczytany,
15 cze 2016, 18:19:4115.06.2016
do 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

nieprzeczytany,
15 cze 2016, 18:39:4415.06.2016
do 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

nieprzeczytany,
15 cze 2016, 18:48:3715.06.2016
do joomla-de...@googlegroups.com
No need for the double quotes around "document":

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


ghazal

nieprzeczytany,
16 cze 2016, 05:15:5816.06.2016
do 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

nieprzeczytany,
16 cze 2016, 08:14:2016.06.2016
do 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

nieprzeczytany,
16 cze 2016, 08:29:2216.06.2016
do 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

nieprzeczytany,
16 cze 2016, 09:35:5016.06.2016
do 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

nieprzeczytany,
16 cze 2016, 09:43:3416.06.2016
do 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

nieprzeczytany,
16 cze 2016, 10:00:2116.06.2016
do 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.
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Wiadomość została usunięta
Nowe wiadomości: 0