Home > Web Front-end > JS Tutorial > body text

Introduction to precautions for loading public js at the bottom of the page_javascript tips

WBOY
Release: 2016-05-16 17:28:18
Original
913 people have browsed it

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:

Copy code The 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 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:
Copy the code The code is 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.
Copy code The code is as follows:

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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template