I h ave this code:
drupal_add_js("var thu = " . drupal_to_js($key) . ";", 'inline');

However when I load the page and view the source, I don't see any JS that says var thu=35363 ( for example )

Check the web developer JS error log, I see that thu is undefined. Can Drupal pass PHP variables to Javascript?

Any help would be appreciated.

Comments

danbh’s picture

Hey there, I can't tell whats going wrong from what you got there. Everything looks ok. The use of drupal_to_js is cool. I didn't know about that function. I have been using var_export to convert a boolean to a string. Here is a line of my code, that has been working for me:

drupal_add_js('var honey_perm = '.var_export(user_access('access king honeycomb'),true).';' , 'inline');

It looks to me, that what you have there should be working. Try scrapping the call to drupal_to_js, and just putting a dummy value; just to check that the add_js is actually being called. I suspect that for some other reason, its not being called.

Good luck

stevenpatz’s picture

I've taken out the call to drupal_to_js and used a dummy value and still nothing.

Somehow I think that drupal_add_js isn't being called.

Island Usurper’s picture

I'm having the same problem doing the same thing: defining a JavaScript variable from some PHP.

What's maddening is later in the function is another call to drupal_add_js(), and that code is being loaded. Both uses are 'inline', so I don't know what would be the problem.

-----
Übercart -- One cart to rule them all.

khoomy’s picture

I also used drupal_add_js function and it is working fine. here is my code

$categories = search_get_category_list();
drupal_add_js("
function display_category_list(){
$('#cat_dropmenu').html(".drupal_to_js($categories).").show(); }"
,'inline');

Khurram Fraz

jdl100’s picture

That works great!

jdl
Learn Spanish
www.edufone.com