Hello there,
I am working on my module and need to introduce some jQuery functionality to it.
After reading the stuff across net I wrote following in my myModuleName.js file
Drupal.behaviors.myModuleName = function(context) {
$('document',context).alert ("function called");
}I also added following to my myModuleName.module file
function myModuleName_init() {
theme('myModuleName_javascript');
}
function theme_myModuleName_javascript() {
drupal_add_js(drupal_get_path('module', 'myModuleName') . '/myModuleName.js');
}Now when I go to a page belonging to my module, I see that page does have drupal.js and myModuleName.js included in <head>
But I still don't see the confirmation (through the alert I wrote in myModuleName.js file) coming.
For alert I tried writing alert(), document.alert(), $(document).alert(), $(document,context) etc. but none worked.
Am I doing something wrong? Is something incomplete? Is there any other way I can check whether Javascript is working or not?
Thanks,
Mukesh
Comments
Unable to make javascript work in my module
Hello Mukesh
Instead of this
Drupal.behaviors.myModuleName = function(context) {
$('document',context).alert ("function called");
}
can we use the the following code
$(document).ready(function() {
alert("hello World !!!);
});
http://www.module-node.com
Hello Jenifer, Thanks for the
Hello Jenifer,
Thanks for the reply.
However your reply seems to suggesting opposite of what I read everywhere.
I read that we should not use document.ready() now as Drupal has a wrapper around it which calls that.
You can refer to : http://drupal.org/node/205296 and http://viziontech.co.il/tutorial1
Thanks,
Mukesh
By using Drupal.behaviors,
By using Drupal.behaviors, you are indeed using the correct Drupal terminology for calling javascript on page load.
How about simplifying it a little:
Drupal.behaviors.myModule = function(){
alert("here");
}
Does that work?
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
shouldn't this : <?php
shouldn't this :
<?php
function myModuleName_init() {
theme('myModuleName_javascript');
}
function theme_myModuleName_javascript() {
drupal_add_js(drupal_get_path('module', 'myModuleName') . '/myModuleName.js');
}
?>
be like this:
<?phpfunction myModuleName_init() {
drupal_add_js(drupal_get_path('module', 'myModuleName') . '/myModuleName.js');
}
?>
if you use the first one, you need to invoke hook_theme as well
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
skype id : duckzland
Solved
Hello Jay & Jason(duckzland),
I was using hook_theme() too. I forgot to mention it in my post.
Actually problem turned out to be in some extra code that I had put in the same function. I was making a call to file_directory_path() which is a php function, and I guess that's why it created a problem here.
Doesn't Javascript provide any kind of compiler error mechanism saying "function not defined" or some such?
Another problem now I have to figure out is how to do the equivalent of what I was intending to do with
file_directory_path(), in JS.
Thanks for your help.
Thanks,
Mukesh
javascript is not compiled,
javascript is not compiled, so no, it doesn't provide that.
However, if you use the firebug extension for firefox, it will inform you of such errors. I recommend it fully.
To get your file directory path you need to pass it as a setting to your javascript function:
PHP:
<?php$settings = array('fileDirectoryPath' => file_directory_path());
drupal_add_js(array('myModule' => $settings), 'setting');
?>
Now the filepath is available by using the following in your javascript:
var filepath = Drupal.settings.myModule.fileDirectoryPath;Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Thanks Jay.
Thanks Jay.
Hi All, A new problem in
Hi All,
A new problem in similar field: So only way I can add onload functionality is by writing "
Drupal.behaviors.ModuleName = function ()" ?If yes, then how do I execute different onload() code for different pages inside a module?
I encountered this problem while I was trying to add jquery tabs in my page and for that I had to write
$(#tabsToShow).tabs()to a page. However not all page would have the same requirement. So I don't want to write it in the common code that would be executed for each page of the module.Also, another issue was that
$(#tabsToShow).tabs()didn't work even when I put it in the generic code for the module :( I am using jquery_ui module. I wonder what wrong am I doing there.Thanks,
Mukesh
PS: Ducks, I tried to add you on my Skype. Did you get my request?
Only include the code on
Only include the code on pages where you want it to load. Write separate scripts for separate pages.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Jay, Thats what I am doing. I
Jay,
Thats what I am doing.
I have one "moduleName.js" in which I have written the above mentioned code. And then I have done drupal_add_js() for that file in the moduleName.module file. That seems to work correctly.
However I have another page in the module with name "showbook". Now I created a MENU_CALLBACK function using hook_menu and named it showBookHandler(). I have defined that function in an include file named moduleName.showbook.inc . I have also mentioned that filename in the hook_menu.
Everything seems to be working.
Now I want to do something specific on the onload of '/showbook' page. So I created a file called moduleName.showbook.js and called drupal_add_js in the showBookHandler(). And in this js file I wrote code
Drupal.behaviors.showbook = function() { alert ("hello"); }But this doesn't seem to work. Why?
Thanks,
Mukesh
If you have called both using
If you have called both using
Drupal.behaviors.showbook, it's essentially the same as doing this:a = 1;a = 2;
a will equal 2, because you are overwriting the first value with the second value. If you are going to include the same onload code twice on a page, you will have to place them in a separate property of the Drupal.behaviors object.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Well of course I haven't done
Well of course I haven't done that.
In my moduleName.js I have written
Drupal.behaviors.moduleName = function(), while in my moduleName.showbook.js I have writtenDrupal.behaviors.showbook = function ()Hey it works now. I figured
Hey it works now. I figured that I was calling .live() function of jquery in my code while Drupal (even after jquery_update) currently only supports till jquery 1.3
I am sorry I am troubling you people for my stupid mistakes, but when things go wrong it is tough to figure what would be exact stupidity.
Btw I have been using Firebug since quite some time. But it didn't throw any error for all these JS errors. I even enabled the "net" module of that. I also have JSVIew add-on enabled, not sure whether that is required though.
you can use livequery jQuery
you can use livequery jQuery plugin to emulate the .live() function or user drupal attach behaviors
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
skype id : duckzland