I'm working in my .js file. When my function, mainPaginationClicked is called, I want it to also execute another function, rotateMessage. rotateMessage is declared in the script tags of my html document. Is there a way to call this function from my .js file?
So long as you load the JavaScript file after the function is delcared in the HTML (or only invoke the function after it is declared), then the function you declare will be available within the same JavaScript context. The actual file location doesn't matter, only the in-memory namespaces.
The default behavior in a browser is to block while loading JavaScript using script tags, so you can safely write the JavaScript resources in the order that you want them executed without worrying about the fact that the external file needs to be loaded (and unless explicitly controlled otherwise they all get pooled together in a global namespace).
Yes, you can call this as it would be in your .js file. Functions in JS are shared between files, as variables are, too.
Provided you make sure your js file is included after the script tag, there should be no problems with invoking rotateMessage
来源:https://stackoverflow.com/questions/13054616/calling-js-function-declared-in-html-file-from-a-js-file