简体   繁体   中英

Proper way to include one js file in another

I tried both from this answer and none of them work:

$(function () {
  var imported = document.createElement('script');
  imported.src = '/assets/admin/receipt_zooming.js';
  document.head.appendChild(imported);

  $('.rotate-receipt').on('click', rotateImage());
  $('.zoom-in').on('click', zoomHandler('+'));
  $('.zoom-out').on('click', zoomHandler('-'));

// OR

  $.getScript('/assets/admin/receipt_zooming.js', function()
  {
    $('.rotate-receipt').on('click', rotateImage());
    $('.zoom-in').on('click', zoomHandler('+'));
    $('.zoom-out').on('click', zoomHandler('-'));
  });
});

receipt_zooming file:

$(function () {
  return function() {
  var zoomHandler = function(sign) {
    return function() {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-' + index);
      switch (sign) {
        case '+':
          image.width(image.width() + 100);
          break;
        case '-':
          image.width(image.width() - 100);
      }
    };
  };
  var rotateImage = function () {
    var angle = 0;
    return function() {
      var index = $(this).data('button-index');
      angle = (angle + 90)%360;
      var className = 'rotate' + angle;
      $('#receipt-image-'+index).removeClass().addClass(className);
    };
  };
}
});

I need to include the receipt_zooming.js in a few another files. Please help.

edit: Can I know why someone -1 instead of helping?

function loadScript(file) {
   var imported = document.createElement('script');
  imported.src = '/assets/admin/receipt_zooming.js';
  imported.type = "application/javascript";
  document.head.appendChild(imported);       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM