0

I've looked at a few posts on stack overflow saying that this should work.

<script type="text/javascript" src="fcn1.js"></script>
<script type="text/javascript" src="fcn2.js"></script>

The scripts have for example

fcn1.js:

function myFunc() {

    alert('This script accessed by other script');

}

fcn2.js:

myFunc();

When I try to do this I get a referenceError, saying MyFunc() is not defined.

The scripts are loaded in order like that, is there an equivalent for $("#something").on("load", function() { }); that I could use to wait for the first script to load?

In my case, script 1 has 702 lines so it could be possible that it's not loading fast enough? I wouldn't think so because those functions that I'm trying to access by an external file are triggered almost immediately upon first load of the page. (not triggered by the second script, but they are triggered within the first script).

  • 2
    You shouldn't get an error saying that `MyFunc()` is not defined. The script *fxn2.js* shouldn't run at all since you put the start tag that loads it inside the start tag for the previous script. – Quentin May 31 '16 at 12:49
  • "could be possible that it's not loading fast enough" — No. It isn't an async or defered script. – Quentin May 31 '16 at 12:49
  • i.e. you are missing the closing `` tags – devnull69 May 31 '16 at 12:50
  • Read [this very important guide to putting code into your questions](http://stackoverflow.com/help/mcve). – Quentin May 31 '16 at 12:50
  • @devnull69 Sorry I do have the closing script tags, I didn't show them on here though, I'll edit that. – joehungjohn May 31 '16 at 12:52
  • 2
    I bet that the problem is that `MyFunc` is out of scope, but we can't tell because not enough of *fcn1.js* has been included. – Quentin May 31 '16 at 12:52
  • The closest to delayed execution you have in plain JS is window.onload = someFunction; but keep in mind that by default setting it overrides previous handlers. – rdmptn May 31 '16 at 12:54
  • Any errors on the console? Maybe a simple typo? It should work like this – devnull69 May 31 '16 at 12:55
  • I am looking at the console. I am using an image preloader (about 30 images) and I am using a $(window).on("load", function( { } ); to wait for those images to load. But then the functions run and everything within the same file. It's when I try to access those functions that have ran, from an external javascript file(the second one) that the error is thrown that the functions aren't defined. – joehungjohn May 31 '16 at 12:58
  • http://stackoverflow.com/help/mcve – Quentin May 31 '16 at 12:59
  • It looks like scope issue BUT like said above, you didn't provide enough info. Now you can [read it](http://ryanmorr.com/understanding-scope-and-context-in-javascript/) – A. Wolff May 31 '16 at 13:00
  • I'm giving 1:4 for the function being defined inside a window.load or a document.ready callback. – JJJ May 31 '16 at 13:07
  • @Juhana yes the function is defined inside a window.load, due to an image preloader, because the functions are dependent on the images existing, and there are many images(30). Maybe I can define them outside of the window.load, then call them from inside the window.load... that didn't help either, the code didn't break... maybe I have to rethink about re-structuring my code. – joehungjohn May 31 '16 at 13:14
  • Well, that's your problem, and your solution. Defining functions inside the callback makes them local to that function only. You don't need to wait for anything for *defining* the functions, just as long as you don't *call* them before everything's ready. – JJJ May 31 '16 at 13:18
  • I closed the window.load just for the images themselves for the preloader which uses a for loop to loop through an array of image sources. I'm not sure if encasing that inside a window.load is the same as say $("#image").on("load", ... anyway I'm going to see if I can redo the code another way... I broke it up because I couldn't think of an easy alternative like including a file using say AJAX. I'll keep what you said in mind. – joehungjohn May 31 '16 at 13:20

0 Answers0