简体   繁体   English

从加载的页面调用js页面

[英]calling a js page from a loaded page

It's a bit complicated and trying to provide code would be posting 4 pages of codes, but maybe someone could help me think: I have index.php with a div that's loading (jQuery .load()) another page, board.php, which show's new messages it reads from a mySQL table. 这有点复杂,尝试提供代码将发布4页代码,但是也许有人可以帮助我思考:我有一个index.php,其中的div正在加载(jQuery .load())另一页board.php,其中show从MySQL数据库读取的新消息。 All the JavaScript for the entire site is in a file called other-js.js. 整个站点的所有JavaScript都在一个名为other-js.js的文件中。 The JS functions for the message board are written inside the board.php file (because when I written them in the other-js.js file they didn't work). 留言板的JS函数写在board.php文件中(因为当我将它们写在other-js.js文件中时它们不起作用)。 Now I need a function from the board.php file to call a function from the other-js.js file, but they just won't do it. 现在,我需要board.php文件中的函数来调用other-js.js文件中的函数,但是他们只是不这样做。 Any ideas? 有任何想法吗? I'm really at a loss here... 我真的很茫然...

A variable or function declared in the global scope ( window in browsers) should be accessible to all your JS code. 在全局作用域(浏览器中的window中声明的变量或函数应可用于所有JS代码。 So, just attach your function declared in other-js.js file to the window object. 因此,只需将您在other-js.js文件中声明的函数附加到window对象。

eg 例如

window.my_function = function() {...}

Remark: polluting the global scope could be a nightmare in the long term. 注:从长远来看,污染全球范围可能是一场噩梦。 I don't know if you can rewrite your application, but if you can I suggest to put all your JS in files (eg main.js , board.js , etc.) and use modules or namespaces to avoid pain. 我不知道您是否可以重写应用程序,但是如果可以,我建议将所有JS放在文件中(例如main.jsboard.js等),并使用模块或命名空间来避免痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM