How to add Drupal data to Javascript using drupal_add_js

There are times when one will need to pass Drupal data in Javascript or JQuery. One example that comes to mind is for analytics purposes, say after a user clicks on a specific link, you’d want to find the node type of the page the user made this click.

In your .tpl.php, template.php file or anything similar that deals with your theme, you can add the following (Drupal 6 installation):

drupal_add_js(array('myvar' => $node->type.'|sometextifneeded'), 'setting');

Then, in your JavaScript file, using JQuery, you can add the following within a specified namespace (remember to reference your jquery file):

Drupal.behaviors.mynamespace = function () {
$("#nav li.about").bind("click", function() {
var100 = Drupal.settings.myvar;

Please comment if you have questions!


Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically each day to your feed reader.

Trackbacks & Pingbacks

albuquerque roots…

blog topic…

den noi that q4…

How to add Drupal data to Javascript using drupal_add_js |…

new jersey…

blog topic…


How to add Drupal data to Javascript using drupal_add_js |…


Tremendous issues here. I am very satisfied to peer your article.
Thanks a lot and I’m taking a look forward to contact you.
Will you please drop me a mail?

B?n th??ng làm cho gì ?? kh?c ph?c ph? li?u t?i nhà
hay t?i n?i làm cho vi?c? s? h?u ph?i lúc th?y
ph? tru?t li?u quá ph? thông, b?n s? g?i cho 1 ng??i nào ch?m tri?n khai thu s?m ??ng nát trên ???ng ??
bán h?t ?i, nh?ng giá c? thì tùy m?i ng??i và tùy
t?ng th?i kh?c. s? h?u th? b?n s? không quan tr?ng v?
giá, nh?ng ?? ph? tru?t li?u là ?? tái ch? ???c, và c?ng là trong kho?ng nh?ng
?? v?t b?n ?ã s?m v? s? d?ng, v?y vì sao b?n không mua m?t ??a ch? thu tìm tru?t ph? li?u giá cao ?? s? h?u l?i h?n? tru?t ph? li?u Linh Anh là ??a ch? mà b?n s? h?u th? tin t??ng ?? thu
s?m tru?t ph? li?u và ?ng ý v? giá c?.

Hello, this weekend is nice for me, since this occasion i am reading this enormous
educational piece of writing here at my residence.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: