0

I was wondering are there any restrictions regarding sharing global variables between two scripts if I set the value by a $.get approach?

My code of script 1:

<script type="text/javascript">
        var get_data;
        $.get('website_url', function(data) {               
               get_data = "show me the data";
               console.log("1", get_data);
           });
        console.log("2", get_data);
    </script>

My code in script 2:

<script type="text/javascript">
    console.log("3", get_data) 
</script>

The output of my console.logs are as follows:

  • 2 undefined
  • 3 undefined
  • 1 show me the data

I do not understand why the global variable is not filled with the "show me the data" string. Can anyone help me or tell me why this does not work?

Rotan075
  • 2,567
  • 5
  • 32
  • 54
  • `$.get` is __asynchronous__ call – Satpal Sep 16 '15 at 12:37
  • 1
    read all about $.get in the documentation - https://api.jquery.com/jquery.get/ - pay specific attention to the `success` callback parameter, or the section named **The jqXHR Object** and especially the `Promise` interface ... it's not quite Promise/A+, so, it's more of a broken Promise (pun intended) - but it's functionally similar so it's all good – Jaromanda X Sep 16 '15 at 12:42
  • Thank you! I will have a look! :) @JaromandaX – Rotan075 Sep 16 '15 at 12:46

0 Answers0