CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • CiviCRM js breaks Drupal-triggered jquery plugins?
Pages: 1 2 [3]

Author Topic: CiviCRM js breaks Drupal-triggered jquery plugins?  (Read 20359 times)

otiteca

  • Guest
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
December 02, 2009, 02:04:34 am
Ok i did experience the same problem than you.
So to solve it i did change the code that call the jquery plugin in my drupal theme, and so, all my js is working, even on the civicrm pages.

Do that:

Replace: 
 $(document).ready(function(){
    $("ul.sf-menu").superfish();
  });

by:

  jQuery(document).ready(function($){
    $("ul.sf-menu").superfish();
  });


An it should work now.

Cheers

Olivier


Quote from: greenmachine on July 22, 2009, 12:27:34 am
I've encountered this with two sites, running Drupal 6 and CiviCRM 2.2.5 or 2.2.7. It seems like there is something about how CiviCRM instantiates its own copy of jquery that causes subsequent loading of jquery plugins to fail. My Drupal code loads the plugin js file OK, but actually trying to run the function provided by the plugin causes a "is not a function" error in the javascript console. Meanwhile, other inline code using the core jquery methods (like $("#this").hide();) in my Drupal code works fine.

Here's an example from the resulting page markup:

Code: [Select]
<script type="text/javascript" src="/sites/all/modules/civicrm/packages/jquery/jquery.js"></script>

... ( several lines lower ) ...

 <script type="text/javascript" src="/misc/jquery.js?n"></script>

... ( several lines lower, getting to my code ) ...

<script type="text/javascript" src="/sites/all/themes/fairvotemn/js/superfish.js?n"></script>
<script type="text/javascript">
<!--//--><![CDATA[//><!--

  $(document).ready(function(){
    $("ul.sf-menu").superfish();
  });
 
//--><!]]>
</script>

I've tried it with three different jquery plugins (superfish, timers, curveycorners) that all work fine on non-CiviCRM pages, so I don't think the plugin code is the problem here. I've also tried different plugin function invocations like jquery(element).superfish(); and cl(element).superfish();

You can see an example of this problem (for the moment) here:

http://fairvote.reinventingthe.com/civicrm/profile/edit&gid=9&reset=1

Is the CiviCRM invocation of jquery hijacking later plugin loading (meant for the Drupal invocation of jquery)? Can I excise CiviCRM's invocation so my site is not loading jquery twice on some pages?




scubaguy

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
September 02, 2010, 08:52:07 am
My solution was to add an IF statement to my js file.

   if($("body").hasClass("section-civicrm")) {
      cj('#my-div').myPlugin();
   } else {
      $('#my-div').myPlugin();
   }

After that everything worked again

ehanuise

  • I post occasionally
  • **
  • Posts: 85
  • Karma: 2
  • CiviCRM version: 4.1.3
  • CMS version: Drupal 7.17
  • MySQL version: 5.1.49-3
  • PHP version: 5.3.3-7+squeeze3
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
March 30, 2011, 05:32:34 am
Quote from: scubaguy on September 02, 2010, 08:52:07 am
My solution was to add an IF statement to my js file.

   if($("body").hasClass("section-civicrm")) {
      cj('#my-div').myPlugin();
   } else {
      $('#my-div').myPlugin();
   }

After that everything worked again

Could you tell me which file is the 'js' file you're mentionning ? I searched the various theme, template, libraries, modules, and civicrm files and couldn't find it for sure.

I have several civiCRM functions that do not work (add/remove dashlets to dashboard) unless I disable the jquery_UI module.
However that module is required by other functions on the site (date_popup).
Removing all jquery_ui modules in order to have civiCRM work on the site is not an option, as it would remove functionnality our users rely on and would leaveus open to further incompatibilities when implementing new modules.

As a newcomer, I find this whole thing a huge mess, to be honest.
I can understand that developpers want to use the latest version of some tools, however if the core drupal comes with a given version of Jquery (or other libs for that matter), great care should be taken not to introduce compatibility problems of this kind  :-[

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
March 30, 2011, 09:53:21 am
Yeap, there is an issue of using different jquery within the same pages, one that's  can't logically be solved I'm afraid (they are fighting for the same namespace).

Anyway, instead of

Quote from: scubaguy on September 02, 2010, 08:52:07 am
My solution was to add an IF statement to my js file.

   if($("body").hasClass("section-civicrm")) {
      cj('#my-div').myPlugin();
   } else {
      $('#my-div').myPlugin();
   }

After that everything worked again

To be more robust, I would suggest you to test on the function you want you use instead of a class, eg:
Code: [Select]
if (typeof cj !="undefined")

But nice trick, thanks for sharing.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

ehanuise

  • I post occasionally
  • **
  • Posts: 85
  • Karma: 2
  • CiviCRM version: 4.1.3
  • CMS version: Drupal 7.17
  • MySQL version: 5.1.49-3
  • PHP version: 5.3.3-7+squeeze3
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
March 30, 2011, 11:20:13 am
Could you please post the path of the js file to modify, or at the very least the filename ?

plomax

  • I’m new here
  • *
  • Posts: 1
  • Karma: 0
  • CiviCRM version: 3.4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.36
  • PHP version: 5.2.11
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
May 17, 2011, 08:28:39 am
Posting in this thread as it came first in a Google search. Figured out most of the problems are caused by many of Drupals javascript files being inserted *after* the CiviCRM jQuery file has been included.

This causes conflicts if any of those files use the $ namespace, because they assume you are using the most recent jQuery file before the noConflict() function changes the namespace to 'cj'.

There are likely cleaner fixes for this, like changing var cj = $.noConflict into an included file immediately after the Civi js files are included, rather than an 'inline' function. Because 'inlines' are printed out towards the end which is too late.

There is a hacky fix I posted in the Admin modules Issue queue, you'll have to search for it as I can't post links.

Most of this was trial and error for me as i'm not an amazing programmer, so I could be wrong about much of this, but it does work.

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
May 19, 2011, 03:07:21 pm
which file to modify?  we have the same problem where dashboard js will not work (ie. no drag/drop)

Masked Marauder

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 1
  • \__^..^
  • CiviCRM version: 4.5.5
  • CMS version: drupal-6.34
  • MySQL version: 5.5.40-36
  • PHP version: 5.4.34
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
August 05, 2011, 03:39:32 pm
Quote from: plomax on May 17, 2011, 08:28:39 am
Posting in this thread as it came first in a Google search. Figured out most of the problems are caused by many of Drupals javascript files being inserted *after* the CiviCRM jQuery file has been included.

In my case the civicrm is loaded well after my module's js.

Is there any way to control loading order, or wait until civicrm is loaded?

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
August 08, 2011, 08:52:03 pm
I was bumping into the same problem the other day.

I think the root of the problem is that (a) jQuery's design for multi-version compatibility requires loading <script> elements in a specific order but (b) drupal_add_js (which is used by civicrm.module) does not guarantee <script> order. (This is true in D6. In D7, it can be debated: there is a "weight" option but it seems like it would require on-going twiddling.)

To be more precise, drupal_add_js() is deterministic (given consistent inputs, it will produce consistent outputs), and the order is based on (1) whether the file is included or embedded and (2) the order of drupal_add_js() calls. However, the order of drupal_add_js() calls is hard to predict. For example, if you have two blocks which both depend on <script> elements in the <head>, and if both hook_block implementations call drupal_add_js(), then the order will depend on the visual placement of the two blocks (left/right/top/bottom; first/second/third).

Consequently, the <script> order is predictable for any given configuration (e.g. "out-of-the-box, vanilla Drupal+CiviCRM") but it becomes unpredictable when you allow for different combinations of blocks, modules, themes, etc.

AFAICS, these are the basic approaches:

  • Enhance drupal_add_js to provide more robust ordering-guarantees
  • Replace drupal_add_js with something that understands jQuery instances+plugins. (I don't think the current Drupal-jQuery modules address this issue... but there are so many of those modules, and they all seemed 50% done when I last looked, so I can't be certain.)
  • Remove drupal_add_js; manually build the <script> elements and pass them to drupal_set_html_head.
  • Shift the burden to the client with something akin to http://plugins.jquery.com/project/jquery-deps

malks

  • I post occasionally
  • **
  • Posts: 81
  • Karma: 7
  • CiviCRM version: 3.4.x 4.x
  • CMS version: Drupal 6.22
  • MySQL version: 5.1.x
  • PHP version: 5.3.x
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
August 08, 2011, 10:57:50 pm
I think, but don't hold me to it, that in D6 even you can force module weights, it just requires altering database values directly i.e. there is no inteface to do it.

http://drupal.org/node/110238

EDIT: or you could use the util module http://drupal.org/project/util

RobertPattinson

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • moovieland
    • watch mission impossible 4 online
  • CiviCRM version: 3.4
  • CMS version: 4.0
  • MySQL version: 5.0
  • PHP version: 5.2.4
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
December 01, 2011, 05:01:06 am
I really want drupal with working superfish menus to override civi so my integrated site menu system works consistently so disabling jq at drupal level is not a satisfactory solution in my case.

Masked Marauder

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 1
  • \__^..^
  • CiviCRM version: 4.5.5
  • CMS version: drupal-6.34
  • MySQL version: 5.5.40-36
  • PHP version: 5.4.34
Re: CiviCRM js breaks Drupal-triggered jquery plugins?
December 01, 2011, 07:42:49 am
I came up with a serviceable workaround for a problem I encountered that may be related to this.  Its reported here: http://issues.civicrm.org/jira/browse/CRM-8856?focusedCommentId=37822&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_37822

It might suggest a cleaner solution to this and similar problems to somebody who  understands the drupal/civicrm innards better.

Pages: 1 2 [3]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • CiviCRM js breaks Drupal-triggered jquery plugins?

This forum was archived on 2017-11-26.