Using jQuery getJSON with other included JavaScripts gives a ReferenceError

I made a small sample HTML page to run jQuery getJSON. It looks lower (sorry for the sloppiness, it was just a proof of concept, and then added to a larger project):

<script type="text/javascript">

function test() {   
$.getJSON("http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=e999b3a5d47d013b780e8ac255c86266&user_id=24579658@N03&format=json&jsoncallback=?",    
                function(data){
                      $.each(data.photos.photo, function(i,photo){
     $("<img/>").attr("src", "http://farm5.static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg").appendTo("#images2");
    //alert();
        if ( i == 6 ) return false;
      });
});

}

Then I would call this method when something is clicked using

<script type="text/javascript">

                            $(function() {
                                $("#yo").click(test);
                            });
    </script>

This worked fine in a project where the only JS that I included was jQuery and these were the only functions. However, as soon as I added it to my other project, it was through errors. In another project, there are some mootools libraries that I thought could do this. However, even after completely removing the mootools material and using this jquery material, I still get the following error:

ReferenceError: $ not defined

javaScripts, Google , , JQuery Mootools. - , ?

+3
3

, , , - , , undefined $. , , , jQuery noConflict, $.

jQuery, , :

$ = jQuery;

, $ "jQuery" , :

jQuery.getJSON(...

jQuery(function() {
    jQuery("#yo").click(test);
});

, $symbol . , , "jQuery" . , JavaScript- JavaScript, $shortcut . "$", , , "jQuery" . :

(function($) {
        function test() {   
            $.getJSON("http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=e999b3a5d47d013b780e8ac255c86266&user_id=24579658@N03&format=json&jsoncallback=?",    
                        function(data){
                              $.each(data.photos.photo, function(i,photo){
             $("<img/>").attr("src", "http://farm5.static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg").appendTo("#images2");
            //alert();
                if ( i == 6 ) return false;
              });
        });
})(jQuery);

, $, $ , . , jQuery . , , , $ jQuery.

, $jQuery, jQuery.noConflict() (. http://api.jquery.com/jQuery.noConflict/), JavaScript jQuery, JavaScript, , $shortcut .

+5

$ is not defined , jQuery $ . :

+1

$, mootools . 1 .

jQuery.noConflict() → http://api.jquery.com/jquery.noconflict/

, jQuery. jQuery.noConflict(). - jQuery:

$jQ = jQuery.noConflict();

$jQ

$jQ( "#yo" ).click( test );

JS litte JS, '$' "jQuery" :

(function($) {
  function test() {   
  $.getJSON("http://api.flickr.com/services/rest/
  &method=flickr.people.getPublicPhotos&api_key=e999b3a5d47d013b780e8ac255c86266&user_id=24579658@N03&format=json&jsoncallback=?",    
                  function(data){
                        $.each(data.photos.photo, function(i,photo){
      $("<img/>").attr("src", "http://farm5.static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg").appendTo("#images2");
      //alert();
          if ( i == 6 ) return false;
        });
  });
})(jQuery);

This little beauty is called a self-running anonymous function. We define $ as an input parameter, but pass it to jQuery to ensure that this means the variable $.

0
source

Source: https://habr.com/ru/post/1767178/


All Articles