Calling js function declared in html file from a .js file

落爺英雄遲暮 提交于 2019-12-06 14:15:49

问题


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?


回答1:


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).




回答2:


Yes, you can call this as it would be in your .js file. Functions in JS are shared between files, as variables are, too.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!