There are two files with scripts. In one there is a function. And how in another script to call a function that is in the first script? Thanks for answers)

The first script:

function compareInit() { new InSales.Compare({ _textAdd: 'Добавлено', _textLink: 'К сравнению', selector: 'a.add_to_compare', draw: function(products) { if (products.length == 0) { $('.compare-notice').slideDown(); $('#compare_div').slideUp('fast'); } var compareCount = $('#compare_table .item').length; var compareCountNames = 'товар'; if (compareCount > 1) { compareCountNames = 'товара'; } var compareCountMsg = 'Вы сравниваете ' + compareCount + ' ' + compareCountNames $('.compare_count').html(compareCountMsg); if (!$("#compare_table .same").length) { $(".compare_show_similar, .compare_hide_similar").hide(); } else { $(".compare_show_similar, .compare_hide_similar").show(); } var text = ''; console.log(products.length) if (products.length == 0) { text += '<div class="noforcompare">Нет товаров для сравнения</div>'; $('#compare_div').html('<p>Товары для сравнения не выбраны</p><p><a href="/" class="button" title="На главную">На главную</a></p>'); } else { text += '<ol id="compare-items">' $(products).each(function(index, product) { text += '<li class="item odd"><a href="javascript:;" rel="' + product.id + '" class="remove_compare btn-remove">удалить</a><p class="product-name"><a href="/product/?product_id=' + product.id + '">' + product.title + '</a></p></li>'; }); text += '</ol><div class="actions"><button type="button" class="button" onclick="location.href = \'/compares\'"><span><span>Сравнить</span></span></button></div>' } $('.block-compare .block-content').html(text); } }); } compareInit(); $(document).on("click", "a.add_to_compare", function(e) { e.preventDefault(); }); $(document).on("click", ".compare_show_similar", function() { $('.compare_links a').removeClass('active'); $(".compare_show_similar").addClass('active'); $("#compare_table .same").show(); }); $(document).on("click", ".compare_hide_similar", function() { $('.compare_links a').removeClass('active'); $(".compare_hide_similar").addClass('active'); $("#compare_table .same").hide(); }); $(document).on("click", '.compare_clear a', function() { show_preloader(); $.cookie('compare', null); $('.compare-notice').slideDown(); $('#compare_div').slideUp('fast'); hide_preloader(); }); 

The second script:

  $(document).ready(function () { attachClickEvent(); }); var pInfScrLoading = false; var pInfScrDelay = 250; function pInfScrExecute() { pInfScrNode = $('.more').last(); pInfScrURL = $('.more a').last().attr("href"); if(pInfScrNode.length > 0 && pInfScrNode.css('display') != 'none') { $.ajax({ type: 'GET', url: pInfScrURL, beforeSend: function() { pInfScrLoading = true; pInfScrNode.clone().empty().insertAfter(pInfScrNode).append('<img src=\"https://assets3.insales.ru/assets/1/3389/732477/v_1482567929/build/loader.gif\" />'); pInfScrNode.hide(); }, success: function(data) { pInfScrNode.next().remove(); pInfScrNode.remove(); var filteredData = $(data).find(".category-products"); filteredData.insertBefore( $("#product-list-foot") ); $('.pages_my').html($(data).find('.pages_my').html()); compareInit(); pInfScrLoading = false; attachClickEvent(); }, dataType: "html" }); } } $("body").on("click", ".more a", function() { return run_filter($(this).attr("href")); }); function attachClickEvent(){ $('p.more a').click(function(event){ pInfScrExecute(); event.stopPropagation(); return false; }); } 

Run in order - 1 then 2. You need to run the compareInit(); function in the second script compareInit();

  • it all depends on the order of the connection files and the code that is inside. Without this, it is impossible to answer the question - Grundy
  • @Grundy - added - user232660
  • @Aleskandr, your first function ( } ) is not closed. And in the second, why don't you call it in success? - Yuri
  • @Yuri - I apologize, I did not copy everything) When calling in success, the same error) - user232660
  • one
    @Aleskandr, if there are complete files in the question, then everything should work. Try to make a minimal reproducible example reproducing the problem - Grundy

3 answers 3

Script number 1:

 function func() { ... } 

Script number 2:

 func() 

In the head:

 <script src="скрипт №1"></script> <script src="скрипт №2"></script> 
  • Thank you Yuri, did as you said - but the error: futs.is is not defined. The order is not broken. - user232660
  • @Aleskandr, well, I have the same case, I use this and everything is fine with me - Yuri
  • @Aleskandr, can you drop the photo or upload the head code, script 1 and script 2 here? - Yuri
  • @Aleskandr, as far as I remember, you asked at the expense of ajax) - Yuri
  • added script codes - user232660

Upload files in the right order and you will be happy.

In ancient times, the order did not guarantee succession, and even now the situation is a little confused with all sorts of async and defer .

Use defer to specify dependencies or dependencies, of which there are many (although in their pure form there are already a few).

View (snippet does not support files) .

    As already mentioned, the order of loading scripts will be guaranteed if you enter the defer attribute:

     <script src="скрипт №1" defer></script> <script src="скрипт №2" defer></script>