0

i'm following along a book and I have 2 javascript files. Both are in the HTML file as:

<script src="playlist_store.js"></script>
<script src="playlist.js"></script>

however, when I try to call a function from playlist_store.js inside of playlist.js chrome debugger says "uncaught reference error. loadPlaylist is not defined. loadPlaylist() is the function from playlist_store.js

I thought there might be a typo somewhere but I don't think there is. Does playlist.js actually need to somehow import or include / require the playlist_store.js ? Is the browser not smart enough to somehow link them so the functions in one you can call from another?

3
  • order of loading the files? does it help if you reverse the includes? Commented Nov 9, 2012 at 17:13
  • 4
    the function in playlist_store.js may be not in the global scope. Commented Nov 9, 2012 at 17:14
  • Post the code in playlist_store.js. You don't have to do any importing. If loadPlaylist is actually defined as a function in reachable scope, it should work. Commented Nov 9, 2012 at 17:15

1 Answer 1

2

Defining a function like follows

function myaction () { }

is a named (private) function.
It is better to write a (private) function assigned to a variable:

var myaction = function () { };

Now what you need is a function, that is assigned to a variable in the global object:

myaction = function () { };
// or strict:
window.myaction = function () { };

It is now a global (public) function, so you can use it outside of your defining .js.

Beware that the first example, a function statement doesn't have a semicolon. Using the function operator as in the other examples, you need a semicolon.

Sign up to request clarification or add additional context in comments.

1 Comment

I defined the function like the first option function myaction() {}. Apparently though when I copied and pasted from the PDF file to expression, the "" quotes became different and it had some error which prevented the playlist_store.js from loading and caused the error. I found it in the chrome debugger. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.