user3187759 user3187759 - 2 years ago 68
Javascript Question

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.
All the JavaScript for the entire site is in a file called 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).
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.
Any ideas? I'm really at a loss here...

Answer Source

A variable or function declared in the global scope (window in browsers) should be accessible to all your JS code. So, just attach your function declared in other-js.js file to the window object.


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 (e.g. main.js, board.js, etc.) and use modules or namespaces to avoid pain.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download