Drupal 7 における jQuery

Drupal 7 では jQuery の振る舞いがちょっと Drupal 6 のそれとは異なるようで、少し手こずりました。

まず、通常 Drupal.behaviors で jQuery の全コードを囲むと思いますが、Drupal 6 では


Drupal.behaviors.module_name = function(context) {
  your code goes here...
};

となっていたことと思いますが、Drupal 7 では


Drupal.behaviors.module_name = {
  attache: function(context) {
    your code goes here...
  }
};

という風に変わります(参照:Converthing 6x modules to 7.x - Changed Drupal.behaviors ...

また、通常 jQuery では、以下のように $(ドルサイン)を使ってコマンドを書き始めます。


$(document).ready(function() {
  alert("Hello World");
};

しかし、Drupal 7 に含まれる jQuery では、この $ マークで jQuery コマンドを書き始めるということができないようです。

この問題を解決するには2通りの方法があります。1つ目は、$ の代わりに jQuery という文字列を用いるというものです。


jQuery(document).ready(function() {
  alert("Hello World");
};

もう一つは、以下のように jQuery のコード全体を囲う、というものです。(参考:Converting 6.x modules to 7.x - JavaScript should be compatible with other libraries than jQuery


(function($) {
  $(document).ready(function() {
    alert('Hello World.');
  })
})(jQuery)

以上をまとめると、Drupal 7 向けに jQuery を使うには、以下のようにするのが基本、と言えそうです。(コメント文からした3行は任意に書き換える)


Drupal.behaviors.your_module = {
  attach: function(context) {
    (function($) {
      // your code goes here ...
      $(document).ready(function() {
        alert('Hello World.');
      })
    })(jQuery)
  }
}

Comments

Obat Herbal Pendarahan Rahim

Telly Serial Updates

All Indian TV Serials are now cover by tellyserialupdates.com website, you can read the full episode written update from this blog. Our website will provide you latest serial gossips news, recap and precap of all upcoming episodes of 2016. written updates. Apparently a lot of the benefits of Information and Communication Technology in education. With the possible occurrence of education and dissemination of information technology to transform science education

This is really nice one to read

حيث تقوم جميع الفتيات الصغار

حيث تقوم جميع الفتيات الصغار بإختيار الكثير من الملابس المختلفة من أجل أن تتنقل باربي مع صاحبتها إلى عالم الخيال حيث السهرات والحفلات, وأيضا التنزه بين الحدائق, ولا يمكن أن تخلو خزانة باربي من ملابس يومية مخملية مدهشة وأنيقة العاب بنات , وقد قام مبتكروا الألعاب بتخصيص أحد العاب الفتيات الخاصة بشخصية باربي من أجل إختيار الملابس اليومية الخاصة بها من خلال لعبة تلبيس باربي المخملية.

juegos de pokemon

 Excellent article.<a href="http://www.juegoscazarpokemon.com/">juegos de pokemon</a>

juegos de pokemon

 Excellent article.http://www.frutijuegos.com/

When I visit this site I see

When I visit this site I see this site is popular and a lot of traffic. I want to learn to make my site as it is. obat amandel | obat cacar air | obat batu ginjal

Written Telly Updates

i like your theme can you please tell me the name of your theme because i want to use your theme on my blog also.you can read the full episode written update from this blog. Our website will provide you latest serial gossips news, recap and precap of all upcoming episodes of 2016. Check More here Shakti Written Updates

I would like to install

I would like to install Drupal 7 in some of my sites. Crazy Bulk reviews

Indian TV Serial Written Updates

well now you will get latest trending news of all daily soaps and we will provide you days by day written updates of all episodes. So now you will get all information at a single spot so please follow this link Kumkum Bhagya Written Updates

My son was very uncomfortable

My son was very uncomfortable because of the same problem then we started taking guidance by them and now he feels much relaxed than ever before! vpn service review

I have mentioned problem of

I have mentioned problem of Jquery in Drupal couple of time but no response so far from any developer.click this link

obat sperma

bragas calvin klein

Clean grill well bragas calvin klein before cooking and oil the grates generously to prevent sticking.

There are some

There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well Berita Bola Prediksi Bola Information

Magnificent website.

Magnificent website. Plenty isabel marant chaussures of helpful info here. I am sending it to several friends ans additionally new balance femme bordeaux sharing in delicious. And naturally, thanks in your sweat!

You should take

You should take part in a contest for one of the best blogs on the web. I will recommend this site! Culbutage.com Diabeq.com Star-glasses.com

If we can only encounter each

If we can only encounter each other rather than stay with each other 192.168.1.1|192.168.1.1|192.168.1.1|192.168.1.1

FB Remarketing 2.0 Biz in a

use ful news

very nice.100% Job Oriented R Programming Training In Chennai for more Information click to  <a href="http://www.greenstechnologys.com/dot-net-course-content.html">the best dot-net training in Chennai</a>

xender for pc

Very use full  and interesting <a href=http://www.xenderforpcapp.com>xender for pc </a>

awesome site

wow such a good site thanks happy dussehra 2016

glad to be acquainted with

glad to be acquainted with you , <a href="http://narwhale-io.com/">game Narwhale-io</a>

Black Magic Specialist Baba

Really Nice Post Thanks For Share.  Black Magic Specialist

Happy New Year 2017

Watch NFL games

Watch NFL Games - You can watch live nfl games 2016 for free online - including the playoffs without paying a huge cable bill every month. NFL Live streaming

An impressive share, I just

An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post! Website Information Visit

This is highly informatics.

This is highly informatics. crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things Explaindio 3.0 ReviewI was searching for Explaindio 3.0 Review

obat kuat pria bagus dan

obat kuat pria bagus dan murah cialis asli. obat kuat pria bagus dan murah cialis 80mg asli. pembesar payudara Disini produk yang terbukti kualitasnya yang dapat membesarkan payudara anda secara permanen yaitu vakum Pembesar Payudara Permanen. Disini produk yang terbukti kualitasnya yang dapat membesarkan payudara anda secara permanen yaitu alat Pembesar Payudara Permanen. bagi anda yang ingin lebih tahan lama di atas ranjang coba sekarang juga obat tahan lama

Great

This post is very interesting for us because its given as much information about congregational ministry keep sharing more post.  abcya | barney | starfall

instaeasy review have Your

instaeasy review have Your Instagram liking, comment and engage automaticlly

instaeasy review have Your

instaeasy review have Your Instagram liking, comment and engage automaticlly

<strong><a

<strong><a href="http://instaeasyreviewz.com/">instaeasy review</a></strong>have Your Instagram liking, comment and engage automaticlly

monica

Thank you, I've been seeking for info about this subject matter for ages and yours is the best I have discovered so far

Instaeasy reviewInstagram

Instaeasy reviewInstagram accounts comment and engage on instagram by instaeasy Nice to be visiting your blog again, it has been months for meinstaeasy review. Well this article that i’ve been waited for so long instaeasy the worlds leading instagram tool

I really enjoy reading and

I really enjoy reading and also appreciate your work.

Positive site, where did u

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Learn More

buy instagram likes fast

Your topic is great ideas for our business.i am very impress this post.i thank you this post.  buy instagram likes fast

Subject

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Calibri}
This is the type of information I’ve long been trying to find. Thank you for writing this information prayer times

The quality of your blogs and

The quality of your blogs and conjointly the articles and price appreciating.
<a href="http://howtogetridofblackheadsovernight.com/">Anthony</a>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.