3

Background:

(*) I'm a JavaScript dilettante -- I just dabble when I need to.

(*) I have a piece of javascript that I've tested with a dummy page that I loaded by just calling it from a <body onload="..."/>. It does what I want there. (See https://github.com/cwestin/bobt/blob/master/site/style/article.html , and the sibling file http://github.com/cwestin/bobt/blob/master/site/style/bobt.js).

(*) I want to add this onload behavior to my Drupal site.

I followed the instructions on how to create a Drupal 7 module from here: http://drupal.org/node/361112.

However, this seems to be aimed at PHP development of block modules that generate content. All I have is the JS file, which will act on some div classes I put in my content's unfiltered HTML markup.

I also found instructions on managing JavaScript here: http://drupal.org/node/756722.

And, a search on this site found these instructions for including JS: Best way for including javascript/jQuery OnLoad in EVERY page on my site . However, that didn't really seem to answer the question it posed -- that I could tell.

However, having created a module by following these incomplete instructions as best I can, it doesn't look like my JS is getting triggered. A View Source shows that my .js file is being included in the pages, but the transformation it does isn't happening (it did work in my dummy test page). It's possible I'm not translating into jQuery correctly; I've never used that before, and the syntax is clearly not just plain old JS.

Here's the module: https://github.com/cwestin/bobt/tree/master/site/style/sectiontoc . As you can see, in the spooky function($) thingy, I add the required attach() to try to add my behavior to $(document).ready at the bottom. But as I said, it's not getting triggered.

Could anyone please tell me what I'm missing, or what I'm doing wrong here? Thanks in advance for any help.

  • If you load the JavaScript via the theme's .info file, it will load for every single page. Why don't you use drupal_add_js('myfile.js');? – user4162 Nov 23 '11 at 17:13
3

I'm not sure if this is the most correct or most elegant solution, but it works for me. I use this to automatically face out messages on page load.

(function ($) {
        $(document).ready(function() {     
        $("div.messages").delay(10000).fadeOut("slow");
    });})(jQuery)

I use this with a zen sub-theme, and include it in the .info file like this:

scripts[] = js/scripts.js

hope it helps.

-J

  • This did the trick, but I had to rewrite my code to be in jQuery -- that wasn't too bad, I just wasted a lot of time before I tried that as a last resort. Thanks! – Chris Westin Jun 4 '11 at 22:49
3

Putting $(document).ready inside of Drupal.behaviors attach is redundant.

The point of attach is that it will run when the document is ready.

  • Without that, all I have is a variable definition inside attach. How does anything get executed? I tried removing the .ready and just replacing it with stoc.doReady(), but still nothing happens. – Chris Westin May 30 '11 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.