2

I have some JS code but Drupal 7 does not recognize it. I'm getting the following error:

TypeError: $ is not a function

Can anyone help me to make this script work? I'm using jQuery v1.4.4.

<script type="text/javascript">
this.screenshotPreview = function(){    
/* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

/* END CONFIG */
$("a.screenshot").hover(function(e){
    this.t = this.title;
    // this.title = "";    
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");                                
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast");                        
},
function(){
    this.title = this.t;    
    $("#screenshot").remove();
}); 
$("a.screenshot").mousemove(function(e){
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px");
});         
};


// starting the script on page load
$(document).ready(function(){
screenshotPreview('some text');
});
</script>
3
  • You have to include jQuery before your script. Commented May 18, 2014 at 10:28
  • jQuery is included at the top of scripts in the head Commented May 18, 2014 at 10:56
  • 1
    For code that's supposed to handle element, you may want to use Drupal.behaviors. See pbuyle.github.io/blog/2013/11/18/… for a few explanations. Commented May 19, 2014 at 1:23

3 Answers 3

9

Try changing all your instances of the "$" shortcut to "jQuery" and it should work. Calling the screenshotPreview function would for example then look like this:

// starting the script on page load
jQuery(document).ready(function(){
screenshotPreview('some text');
});

Alternatively enclose all your jQuery code in a function with jQuery as a parameter and the $ shortcut should then work.

// We define a function that takes one parameter named $.
(function ($) {
  // Use jQuery with the shortcut:
  console.log($.browser);
// Here we immediately call the function with jQuery as the parameter.
}(jQuery));

(Source: https://drupal.org/node/171213)

Sign up to request clarification or add additional context in comments.

1 Comment

This is also explained quite well in jQuery's own documentation, in the chapter dedicated to writing plugins: learn.jquery.com/plugins/basic-plugin-creation.
4

Drupal 7 provides jQuery in the no-conflict mode, which means that $ is not the jQuery object/namespace. This should not be an issue with properly written jQuery plugins that follow jQuery's plugins authoring documentation.

JavaScript code that expect $ to be the jQuery namespace will not work within a Drupal page. This can be easily solved by wrapping the code in an immediately invoked anonymous function that will alias the jQuery namespace to $:

(function($) {
    // Here $ is the jQuery namespace.
})(jQuery);

Comments

2

Try this: (or let me know if that's not right - but it seems to work for me)

/*******************************************************************
*                    :: Define Your Functions ::
*******************************************************************/ 

(function ($) {
    removeAjaxLoader = function() {
        console.log('removeAjaxLoader');
        $('body').find('.ajax-loader').remove();
        $('body').find('.ajax-loader-icon').remove();
        return false;
    } //removeAjaxLoader

    addAjaxLoader = function() {
        console.log('addAjaxLoader');
        removeAjaxLoader();
        $('body').append('<div class="ajax-loader"></div><div class="ajax-loader-icon"></div>');
        return false;
    } //addAjaxLoader
}) (jQuery);

/*******************************************************************
*                        :: Ready Set Go ::
*******************************************************************/ 
(function ($) {
    $(document).ready(function() {
        console.log('ready complete');
        removeAjaxLoader();

    }); // document.ready
}) (jQuery);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.