I've added JavaScript code to my Drupal 7 theme, but it fail to run as it's expected because css are not properly applied when my function is fired.
So I've tried a minimal implementation to check what's happening.
jQuery(document).ready(function($) {
alert("Start");
}
alert() is fired before the page is fully loaded (i.e. all titles are missing).
How can I prevent this?
-
This question does not seem related to Drupal.avpaderno– avpaderno2011年04月12日 21:20:33 +00:00Commented Apr 12, 2011 at 21:20
-
It is related to Drupal. It's a Drupal site, JQuery is part of Drupal core since 6.x, and this is about Drupal behaviors.amateur barista– amateur barista2011年04月12日 21:44:40 +00:00Commented Apr 12, 2011 at 21:44
-
2The question is clearly related to Drupal and the Drupal way to handle the "page is loaded" event is not the usual jQuery way (see amateur barista's answer). Please, move it back to Drupal Answer.Pierre Buyle– Pierre Buyle2011年04月13日 06:09:28 +00:00Commented Apr 13, 2011 at 6:09
2 Answers 2
In order execute a script after everything has fully loaded you want:
$(window).load(function(){
alert('hello');
});
jQuery.ready() executes as soon as the DOM is ready, which is usually before external resources have loaded.
9 Comments
Read about the Drupal 7 JavaScript API here. You want to give attention to the behaviors section. Behaviors do the same thing you are trying to make in your example, however, when a Drupal page loads, one of the first things it does is instantiate the Drupal object. This object gets extended by every module with behaviors and attributes. When you use Drupal behaviors instead of the standard JQuery on document ready you have accessible in your JavaScript function all the properties and methods defined by other modules.
These tutorials (1, 2) are for Drupal 6, but they give you a good explanation and idea of what Drupal behaviors are.