Code example:
<script>
var data = new Array();
data[0] = 'hi';
data[1] = 'bye';
</script>
<script>
alert(data[0]);
</script>
This gives the following error: data is not defined
How do you make something like this work? Especially if the first <script> block is being loaded on the page by ajax, and the second block is working from it. jQuery solution is acceptable.
Newis not a keyword.Use:
Or, more succinctly:
After your edit you mention that the first script block is loaded asynchronously. Your code will not work as written.
datais a global variable, once it is loaded onto the page. You need to use a callback pattern to properly execute the code.Since you haven’t posted the asynchronous code I am not going to provide a
callbacksample. Though, a quick solution follows: