ERasche has asked for the wisdom of the Perl Monks concerning the following question:

Recommend best practice suggests that javascript be passed as the very last part of a HTTP response, so it can load last of all. This is usually done to increase page load speeds, as javascript can download in the background while content is being rendered.

Is there any easy way to accomplish this with CGI.pm? As it stands, all of the javascript files are sent in the head element, which should result in slower page loads. This is fine in some cases where there's javascript to be executed in the page that requires the libraries to be loaded, but in many other cases (mine included), it would be perfectly fine to have it load last. Do the monks know of any options or settings which would allow this? If it's not a built in feature that's fine, I would just rather use the builtins if they're available. My internet searches turned up nothing on this topic. Cheers!

Replies are listed 'Best First'.
Re: Passing javascript last in CGI
by ww (Archbishop) on Jul 25, 2012 at 21:20 UTC

    "As it stands, all of the javascript files are sent in the head element, ...."

    So, don't put the js in the head. Nothing in HTML4.01 (I don't know about HTML5) bars inclusion of your script(s) (or links to your script(s)) in the body of the html.

    Adapted from http://w3schools.com/dhtml/tryit.asp?filename=trydhtml_intro, where it is included in the head... but cf, this mod:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/T +R/html4/strict.dtd"> <html> <head> <title>js test</title> </head> <body> <h1>Hello.</h1> <p> This is a para with a 1-char indent.</p> <script type="text/javascript"> document.write("Hello World!") </script> <script type="text/javascript"> cc=0; function changeimage() { if (cc==0) { cc=1; document.getElementById('myimage').src="bulbon.gif"; } else { cc=0; document.getElementById('myimage').src="bulboff.gif"; } } </script> <!-- does not pass strict for lack of an alt in the images --> <img id="myimage" onclick="changeimage()" src="bulboff.gif" alt="bulb" + width="100" height="180" /> <p>Click to turn on/off the light</p> </body> </html>

      So, don't put the js in the head. Nothing in HTML4.01 (I don't know about HTML5) bars inclusion of your script(s) (or links to your script(s)) in the body of the html.

      This might be outdated information, but putting things in the body actually meant that many browsers had to request and load the javascript (why, it might contain document.write!) before continuing to parse the page -- hence, slowdown.

      I'm very much unsure how browsers load a javascript in a head block, but any slowdown should only affect the first page load (when the scripts are cached), and the OP's concerns are best mitigated by setting up decent caching behaviour and not storing scripts on any external servers whose performance he cannot control.

Re: Passing javascript last in CGI
by tobyink (Canon) on Jul 25, 2012 at 20:08 UTC
    use 5.010; use CGI ':standard'; print header('text/html'); say q[<!doctype html>]; say q[<title>Hello World</title>]; say q[<h1>Hello World</h1>]; say q[<p>Greetings!</p>]; say q[<script type="text/javascript">window.alert("Hello world")</scri +pt>];
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'