JavaScript script files are loaded at the bottom of the page, which can effectively speed up page loading.
However, the php controller is generally written as follows:
Copy codeThe Code is as follows:
$ This-> load-> view ($ HEADER );
$ This-> load-> view ($ MENU );
$ This-> load-> view ($ VIEW_SHOW, $ data );
$ This-> load-> view ($ FOOTER );
$ FOOTER is a shared template used to load js and css files.
$ VIEW_SHOW as the main template, you may need to write some js Code separately. This Code usually requires public file resources, so it is inconvenient to write js Code to the end of $ FOOTER, $ (document) of jQuery ). ready is useless. In this case, use window. onload as follows:
Copy codeThe Code is as follows:
Window. onload = function (){
(Function ($ ){
Function test () {alert (123 );}
// Or write something bound based on jQuery.
}) (JQuery)
};
However, if you want to call the functions in the window. onload window, for example, you want to call parent. test () in the window's sub-iframe, there will be no results.
In this case, make the function a global variable.
Copy codeThe Code is as follows:
Var test; // global scope Declaration
Window. onload = function (){
(Function ($ ){
Test = function () {alert (123 );};
// Or write something bound based on jQuery.
}) (JQuery)
};
It is safer to change private functions to global functions only when necessary.