1

The problem I am encountering is that when I add the alert command the code works, but if I remove it, it does not work. Very strange.

How can I make it work without alert command?

The code below works with alert, but not without.

  $(function(){ 
                    $('.page-links').click(function() {
                      $('#page').load('page' + $(this).data('target') + '.html');
                    }); 

    $('[data-target]').on('click', function() {
         var NavigationNumber = $(this).attr('data-target'); 


            // If and if only page18 is clicked, run diagram18.js
            if (NavigationNumber == 18) {
                alert(NavigationNumber);
                // Run diagram18.js
                    $.getScript("SensorTables/diagram" + NavigationNumber + ".js", function() {
                        window['diagram' + NavigationNumber]();
                    });
    });

 });
Dler Hasan
  • 233
  • 1
  • 11
  • 4
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – JJJ May 29 '15 at 16:25
  • I think you are executing those scripts without document ready, so without alert, the javascript is not loaded yet at the point. Probably put your code into $(document).ready() or document.addEventListener('DOMContentLoaded', function(){}) – Surely May 29 '15 at 16:30

2 Answers2

1

Sounds like an asynchronous operation is finishing up until you click OK in the alert box, but without it it runs immediately. Do you have a snippet of the async bit before?

benqus
  • 1,119
  • 2
  • 10
  • 24
0

The problem is not with alert box, it happens that till the time you click ok on the alert box the asynchronous request operation finishes, if you do not have that alert box then the code jumps to next line before the asynchronous operation finishes and causes the error

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33