Public JS at the bottom of the page loading considerations introduction _javascript Tips

Source: Internet
Author: User
JavaScript script files are loaded at the bottom of the page to effectively speed up the load on the page.
However, the PHP controller generally writes this:
Copy Code code as follows:

$this->load->view ($HEADER);
$this->load->view ($MENU);
$this->load->view ($VIEW _show, $data);
$this->load->view ($FOOTER);

$FOOTER is a common template for loading JS and CSS files.
$VIEW _show As the main template, you may want to write some of the JS code, which usually need to use the resources of public files, the JS write to the back of the $footer is inconvenient, jquery $ (document). Ready can't use it. At this time, with the window.onload can be, as follows:
Copy Code code as follows:

Window.onload = function () {
(function ($) {
function test () {alert (123);}
or write some binding based on jquery or something.
}) (JQuery)
};

But if you want to window.onload from the inside, like you want to call Parent.test () in the Sub iframe of this window, there is no result.
At this point, you can work around the function as a global variable.
Copy Code code as follows:

var test; Declaration of global Scope
Window.onload = function () {
(function ($) {
Test = function () {alert (123);};
or write some binding based on jquery or something.
}) (JQuery)
};

It is safer to change a private function to a global one only when it is needed.

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.