drupal_add_js inline in a block doesn't add js

sylvaingirard - May 20, 2008 - 11:20
Project:Drupal
Version:5.7
Component:javascript
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

In a block, I try to do a

drupal_add_js("$('#productselect').change(window.location = $('#productselect').val());", 'inline', 'header');

which, as far as I know, should add a script tag in the head section with that piece of code and add script tags to the jQuery library. This, however, doesn't do anything.
If I change 'inline' to 'module', it evidently refers to a js file, named to my piece of code, which of course, doesn't exists. The other references are added also.

#1

sylvaingirard - May 20, 2008 - 12:03
Status:active» closed

It suddenly works... :/

#2

wmostrey - May 22, 2008 - 12:09
Title:drupal_add_js inline doesn't add js» drupal_add_js inline in a block doesn't add js
Priority:normal» critical
Status:closed» active

Actually, I'm also experiencing this, on Drupal 5.7. I'm using drupal_add_js 'inline' and 'module' in a block. The 'module' gets added as expected but the 'inline' is nowhere to be found. The drupal_add_js is called in a hook_filter with 'no cache'. If I look at the exact same code in a node, it works perfectly. If I look at it in a block, the 'inline' is ignored.

Sylvain, are you sure you didn't do anything to make this work? Did you edit some settings, play with the caching, ... Nothing works for me: re-saving the block, clearing cache, ...

#3

zeta ζ - May 26, 2008 - 19:10
Version:6.2» 5.7
Priority:critical» normal

You only have a problem with 5.7, yet you have a critical issue against 6.2 indicating it should hold back a release!

See http://drupal.org/node/45111 for correct use of Priority critical.

Are you sure that the hook_filter is being called? as your code works in a node.

#4

dewolfe001 - May 26, 2008 - 21:31

I am going to dig a little more:

Could it be that this chunk of code :

        $javascript[$scope][$type][] = array('code' => $data, 'defer' => $defer);
        break;
      default:
        $javascript[$scope][$type][$data] = array('cache' => $cache, 'defer' => $defer);

is part of the problem? $data for a file could be equal to "example.js" and be a proper key. [] would never match up with a key; it would only be a new key for each inline call added to the code.

 
 

Drupal is a registered trademark of Dries Buytaert.