0

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?

Community
  • 1
  • 1
J. Doe
  • 65
  • 1
  • 5
  • 1
    `I need to include the receipt_zooming.js in a few another files` What does mean??? You don't include one js file into another js file, you include both files in HTML page... Or you load from js file another one to include on the page – A. Wolff Sep 30 '16 at 08:21
  • use a text editor and add a script tag in the head of your desired page with the src the path to your file – madalinivascu Sep 30 '16 at 08:25
  • i cant include it in the html file, it's a rails app – J. Doe Sep 30 '16 at 08:31
  • 1
    Both method should work but you need to include jQuery. have you got an error? – Yukulélé Sep 30 '16 at 08:34
  • Try this thread: http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file `var imported = document.createElement('script'); imported.src = '/path/to/imported/script'; document.head.appendChild(imported);` – RohitS Sep 30 '16 at 08:37
  • You can do it also on server side – Alexander Sep 30 '16 at 08:39
  • my error is that vars defined in the file to be included are not definied. @RohitS this doesnt work, tried – J. Doe Sep 30 '16 at 08:49
  • @Alexander doesnt work: http://stackoverflow.com/questions/602147/javascript-file-per-view-in-rails – J. Doe Sep 30 '16 at 09:02

1 Answers1

0
function loadScript(file) {
   var imported = document.createElement('script');
  imported.src = '/assets/admin/receipt_zooming.js';
  imported.type = "application/javascript";
  document.head.appendChild(imported);       
}
Pranay
  • 432
  • 4
  • 14
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – andreas Sep 30 '16 at 09:28