I need this to execute on load

$("#accordion").accordion();

I'm still pretty new to js and JQuery, and the only way I could get it to work was when I put it into the actual drupal.js file like so

// Global Killswitch on the element
if (Drupal.jsEnabled) {
// Global Killswitch on the element
$(document.documentElement).addClass('js');
// 'js enabled' cookie
document.cookie = 'has_js=1; path=/';
// Attach all behaviors.
$(document).ready(function() {
Drupal.attachBehaviors(this);
$("#accordion").accordion(); // THIS IS WHERE I ADDED IT
});
}

I am sure this is NOT the right way. Could someone tell me how it should be done? ie the correct way?

Comments

allwires’s picture

I put a typo in the title that completely changed the meaning. I think this is better than just making a new topic. Hope someone knows how to do this. I guess the JavaScript is something most people don't like to mess with in Drupal.

onejam’s picture

I will not hack the drupal.js file. Problems will come when you need to upgrade your Drupal site as it will wipe out your changes unless you remember to add it back into the file.

If you want to add and load an external js script use

scripts[] = script.js

and place this in the .info file.

For

$("#accordion").accordion();

You could just stick this in the HTML head of your page.tpl.php file. Provided that you have load the plugin script for your accordion. Or you could use drupal_add_js

The core jquery script comes with Drupal so you wouldn't need to add this.

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien

allwires’s picture

I tried doing

scripts[] = script.js

In the info file and putting in $("#accordion").accordion();
and the accordion doesn't work then. I made sure to clear the cache too. I tried putting alert('hi') into my script.js file to test, and that DID work. I wonder why the accordion isn't loading through there

onejam’s picture

Perhaps, it's because you are missing the accordion plugin script?

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien