JavaScript script files are loaded at the bottom of the page, which can effectively speed up the loading of the page.
However, PHP controllers are generally written like this:
$this->load->view($HEADER);
$this->load->view($MENU);
$this->load->view($VIEW_SHOW, $data);
$this->load->view($FOOTER);
$FOOTER is a common template used to load js and css files.
As $VIEW_SHOW is the main template, you may need to write some js code separately. If these codes usually need to use public file resources, it is inconvenient to write js after $FOOTER. jQuery’s $(document).ready It’s useless again. At this time, just use window.onload, as follows:
window.onload = function() {
(function($) {
function test() {alert(123);}
//Or write some jQuery-based binding
})(jQuery)
};
But if you want to call the function inside from outside window.onload, for example, if you want to call parent.test() in the child iframe of this window, it is Nothing will come of it.
At this time, just make a change and make the function a global variable.
var test; // Global scope declaration
window.onload = function() {
(function($) {
test = function() {alert(123);};
//Or write some jQuery-based binding
})(jQuery)
};
It is safer to change private functions to global only when needed.