Search code examples
jqueryjcrop

How to check that jcrop is initialized


I would like to know how to check is my Jcrop initialized and then create if if is initialized. How to do this and is this possible ? I have jcrop from this page: jcrop.org

Here is my jcrop code. It is located in function initJcrop so maybe there is some way to check that this function is initialized?

var jcrop_api;

function initJcrop($width, $height)
{
  $('#jc_image').Jcrop({
      allowSelect: false,
      allowMove: true,
      aspectRatio: $width/$height,
  },function(){

    jcrop_api = this;
    jcrop_api.animateTo([0,0,$width, $height]);

  });

};

Solution

  • Well the fast way is to use an object, that containts the jcrop_api and other information about it.

    In this way:

    var jCrop={
    initialized:false,
    }
    function initJcrop($width, $height)
    {
      $('#jc_image').Jcrop({
          allowSelect: false,
          allowMove: true,
          aspectRatio: $width/$height,
      },function(){
        jCrop.initialized=true;
        jCrop.jcrop_api = this;
        jcrop_api.animateTo([0,0,$width, $height]);
    
      });
    
    };
    

    After that, you can check if jCrop is initialized

    if (jCrop.initialized){
    //code here 
    }