Community

Strange problem with jquery in D7 - custom scripts don't work if jquery not called 2nd time

Hi,

in almost every page on my site there is jquery loaded perfectly normal and displayed in the header in the source code.

<script type="text/javascript" src="http://www.example.com/misc/jquery.js?v=1.4.4"></script>

But, when I try to use the custom script code, it doesn't work until I add the same code above the custom script.

E. g.:

<script type="text/javascript" src="http://www.example.com/misc/jquery.js?v=1.4.4"></script>
<script type="text/javascript">Some code goes here</script>

These custom scripts are loaded in the body of the document, so it might cause the problem, but I have different custom script and want to avoid customizing the template for every separate case.

Does someone know if this can be avoided? Thank you in advance!

Comments

How are you adding your

How are you adding your custom scripts in the code? You should use drupal_add_js function to add js and you should use Drupal.behaviours

Read documentation at http://drupal.org/node/756722

Hth,
Sadashiv.

Thank you for the reply. Yes,

Thank you for the reply. Yes, I know that, but would like to avoid adding custom code for many pages that have different scripts.

However, I suppose this would be the code to add:

drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));

Which part is the script code - only alert("Hello!");?

I have found the solution. It

I have found the solution. It was necessary to add jQuery(document).ready statement to the beginning of the script:

jQuery(document).ready(function($){

Now it works without adding the library for the 2nd time.