We all want our web sites to load as quickly as possible. It is also
common for the <head> area of the page to contain a lot of information.
At one time (much older templates), it was common to see pages that typically had:
- A <title> tag,
- One or more <meta> tags,
- One or more links to external stylesheets,
- One or more links to external javascripts,
- And probably some embedded javascripts
html Example
- <head>
- ...
- <script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript"></script>
- <script src="javascripts/jquery.nivo.slider.pack.js"
type="text/javascript"></script>
- <script src="javascripts/lightbox.js" type="text/javascript"></script>
- <script src="javascripts/tabbedcontent.js"
type="text/javascript"></script>
- <script type="text/javascript>
- $(window).load(function() {
- $('#slider').nivoSlider({
- effect:'fade',
- animSpeed:1000,
- pauseTime:6000,
- directionNav:false,
- captionOpacity:0.70, //Universal caption opacity
- controlNav:true,
- keyboardNav:false,
- pauseOnHover:false
- });
- });
- </script>
- <script type="text/javascript>
- $(document).ready(function () {
- $('#nav li').hover(
- function () {
- //show its submenu
- $('ul', this).stop().slideDown(400);
- },
- function () {
- //hide its submenu
- $('ul', this).stop().slideUp(400);
- $('ul', this).stop(true, true); }
- );
- });
- </script>
- </head>
As browsers improved, it became more popular to enhance the page load times by moving
the links to javascripts to the bottom of the page. By placing them
right above the closing </body> tag, the DOM (document object model) can
fully load and the scripts can load after that. It was definitely an improvement.
html Example
- <head>
- ...
- <script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript"></script>
- <script type="text/javascript>
- $(window).load(function() {
- $('#slider').nivoSlider({
- effect:'fade',
- animSpeed:1000,
- pauseTime:6000,
- directionNav:false,
- captionOpacity:0.70, //Universal caption opacity
- controlNav:true,
- keyboardNav:false,
- pauseOnHover:false
- });
- });
- </script>
- <script type="text/javascript>
- $(document).ready(function () {
- $('#nav li').hover(
- function () {
- //show its submenu
- $('ul', this).stop().slideDown(400);
- },
- function () {
- //hide its submenu
- $('ul', this).stop().slideUp(400);
- $('ul', this).stop(true, true); }
- );
- });
- </script>
- </head>
- <body>
- web content goes here
- ....
- <!-- move javascripts here -->
- <script src="javascripts/jquery.nivo.slider.pack.js"
type="text/javascript"></script>
- <script src="javascripts/lightbox.js" type="text/javascript"></script>
- <script src="javascripts/tabbedcontent.js"
type="text/javascript"></script>
- </body>
We wanted to streamline things even more, so we made the embedded
javascript into an external one. Copy the javascript code that is
embedded like in the example below and paste into a plain text editor
like Notepad. In this case, we are working with the code for the Nivo
Slider:
Embedded Javascript
- <script type="text/javascript>
- $(window).load(function() {
- $('#slider').nivoSlider({
- effect:'fade',
- animSpeed:1000,
- pauseTime:6000,
- directionNav:false,
- captionOpacity:0.70, //Universal caption opacity
- controlNav:true,
- keyboardNav:false,
- pauseOnHover:false
- });
- });
- </script>
Remove the opening <script type="text/javascript> line and the
closing </script> line. Save the notepad file as slider.js into the
location where your javascripts are kept. Typically it will be a folder
within your web called javascripts, js, scripts or something similar.
You can do this for each embedded javascript that is in the <head> of
your page.
Now the <head> area has less to load into the DOM, and the scripts
will be loaded last:
Revised HTML
- <head>
- .....
- <script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript"></script>
- </head>
- <body>
- ...
- <!-- javascripts -->
- <script src="javascripts/jquery.nivo.slider.pack.js"
type="text/javascript"></script>
- <script src="javascripts/slideshow.js" type="text/javascript"></script>
- <script src="javascripts/menu.js" type="text/javascript"></script>
- <script src="javascripts/nivo-lightbox.min.js"
type="text/javascript"></script>
- <script src="javascripts/tabbedcontent.js"
type="text/javascript"></script>
- </body>
In our newer templates, all of the javascripts are placed at the bottom of the page. We also have taken several of the smaller scripts and combined
them into a single javascript file. Now all of the CSS goes in the top and the javascripts go in the bottom. It faster and much more efficient.
New Revised HTML
- <head>
- .....
- </head>
- <body>
- ...
- <!-- javascripts -->
- <script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript"></script>
- <script src="javascripts/main.js"
type="text/javascript"></script>
- <script src="javascripts/nivo-lightbox.min.js"
type="text/javascript"></script>
- </body>