从.js文件中调用html文件中声明的js函数 [英] Calling js function declared in html file from a .js file

查看:464
本文介绍了从.js文件中调用html文件中声明的js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的.js文件。当我的函数mainPaginationClicked被调用时,我希望它也执行另一个函数rotateMessage。 rotateMessage在我的html文档的脚本标签中声明。有没有办法从我的.js文件调用这个函数?

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?

推荐答案

只要您在HTML中展开函数之后加载JavaScript文件(或者仅在调用声明),那么你声明的函数将在同一个JavaScript上下文中可用。实际的文件位置并不重要,只有内存中的命名空间。

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.

浏览器的默认行为是在加载JavaScript时使用脚本标记,因此您可以按照您希望它们执行的顺序安全地编写JavaScript资源,而不用担心外部文件需要加载(除非明确控制,否则它们全部汇集在一起在全局命名空间中)。

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

这篇关于从.js文件中调用html文件中声明的js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆