1

At the top of my block, is this line:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

This is the reason why the Gallery formatter does not display correctly.

The entire block contains the following:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>    
<script type="text/javascript" src="http://localhost/nes/sites/all/themes/nesvesti/js/facebook/fb-traffic-pop.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){                       
    $().facebookTrafficPop({
        timeout: 10,
        delay: 0,
        title: "Support Us...",
        message: 'Like the page...',
        url: "http://google.com",
        closeable: false,
        opacity: 90
    });

});

</script>

My question is, is there any way to resolve this conflict?

  • Now I load scripts in <head> of html.tpl.php file, everything works fine, but problem is still there. Why script doesn't work if I don't load jquery.js, Drupal already loads the same jquery? – mixerowsky Jun 28 '13 at 17:14
2

There's a few issues there. Drupal ships with jQuery so no need to include it twice. If you need a later version, use the jquery_update module. Also, JavaScript in D7 should be wrapped in a jQuery namespace as per the documentation here https://drupal.org/node/756722

|improve this answer|||||
  • This is the same version as jQuery in drupal, but if it does not load in the block, does not work... – mixerowsky Jun 28 '13 at 12:48
  • 1
    I finally figured out...problem is solved by using: (function ($) { // All your code here })(jQuery); thanks a lot... – mixerowsky Jun 28 '13 at 18:04
0

If you take your jquery code out of the block, and add it in at the theme level as a drupal behavior, you can guarantee that the code will run regardless of when the content is added to the page.

Content can be inserted after the document.ready has fired, in which case your code will never run.

If you put the code in its own .js file, or a file you use for your utility functions, and add it to your theme.info like so:

scripts[] = js/myscripts.js

Then you can add the code using a proper jquery closure, and within a behavior, like so:

(function ($, Drupal, window, document, undefined) {

Drupal.behaviors.myUtilityHandlers = {

    attach: function( context, settings ) {

            $().facebookTrafficPop({
                timeout: 10,
                delay: 0,
                title: "Support Us...",
                message: 'Like the page...',
                url: "http://google.com",
                closeable: false,
                opacity: 90
            });
        });

})(jQuery, Drupal, this, this.document);

It may look like clumsy overkill, but it will ensure your code plays nicely with everything else. I don't know what happens if you use the .once() function on the plain jquery object like that, but normally you'd wrap your code in the once() function so it won't run every time content is added to the page.

this is covered here: https://drupal.org/node/171213

|improve this answer|||||
  • I'm totally new to all this, so it's very hard to understand all this. I tried in many ways but does not work... – mixerowsky Jun 28 '13 at 14:14
  • I wouldn't worry ... a bit more time here at Hogworts and you'll be casting spells with the best of them. You got this to work after all. cheers – Dom Day Jun 29 '13 at 22:14
  • I try the best I can... – mixerowsky Jun 29 '13 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.