0

I am having some issues loading a JS file within JS. I was hoping to get some help.

so basically we have something like this. I dumbed it down as we do not need all the code for everything in here as its quite complex.

<script type="text/javascript" src="js/mainJSCode.js"></script>
<script type="text/javascript">
var grabCode = [];
if(something==true) {

    function loadScript(url, callback)
    {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        script.onreadystatechange = callback;
        script.onload = callback;
        head.appendChild(script);
    }
    loadScript("js/my.js", runJS);
    function runJS(){
        for(var i = 0;i < 100; i++) {
        grabCode.push(someFunctionInsideHereReturnsStuff());
        }
    alert(grabCode.length); //=100
    };
    alert(grabCode.length); //=0 for some reason
}

So this is basically it I stripped out a lot. I just need to get some stuff from my.js into this js file and use the code from there.

Why is the global var grabCode not being global? Sorry, I am a bit rusty and my scope is way off for some reason.

If there is a better way to load in the my.js so I can access its code directly I would be up for that help as well.

0 Answers0