Considerations for loading public js at the bottom of the page

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.