0

I just read this SE Post where it explains the question asked is similar to what I'm asking. Yslow recommends combining all of your external javascripts, if possible. I've actually taken internal javascripts and made them into an external .js file and put it in my /scripts folder, per some recommendation I found on some website. But this leads me to my 2 (similar) questions; I didn't think separate posts were needed.

(1) - How would I combine javascript codes? Just leave a blank line and then copy and paste the other javascript after the other, and so on, until it's all in one file?

(2) - I have an asp.net website with 5 master pages. 4 of the 5 have 6 javascript files (all minified, some on my own using some compression tool that worked). But I read on a SE post and online that you should load the script(s) after page load. How do you do this on an asp.net website? All my .js files are in the

@bluesmoon gave a nice, thorough answer, but I wasn't able to comprehend it:

To load a script after page load, do something like this:

// This function should be attached to your onload handler
// it assumes a variable named script_url exists.  You could easily
// extend it to use an array of scripts or figure it out some other
// way (see note late)
function lazy_load() {
    setTimeout(function() {
            var s = document.createElement("script");
            s.src=script_url;
            document.body.appendChild(s);
        }, 50);
}

This is called from onload, and sets a timeout for 50ms later at which point it will 
add a new script node to the document's body. The script will start downloading after 
that. Now since javascript is single threaded, the timeout will only fire after onload 
has completed even if onload takes more than 50ms to complete.

I don't really understand what he's saying or how to go about implementing it. So that's all ... I just need help in combining javascripts and loading them (or it) after page load; I don't see anything saying "onload" in my master pages or .aspx pages. Thank you for any help or guidance anybody can offer me!

Community
  • 1
  • 1
Jason Weber
  • 5,653
  • 6
  • 40
  • 73
  • 1
    If you combined and compress your java-script. It will not be easy to read it. I had done "compressing" the java-script file and I did not find any way to De-compress the script. There are may other way of doing this like "g-zip compression" to your files. Also you can add the script at the end of your page. – शेखर Nov 02 '12 at 06:48
  • Thank you for taking the time to respond, @krshekhar! I always have backups of my original javascript files if anything goes wrong, or if something needs to be changed and I think I can do it. I used Fisch's suggestion and put all my .js files just above my closing body tag. The site has 47 pages, and all are functioning normally. But I'll see if it's still worth adding that snippet of code I found on SE (and stated in my question). Thank you again, and I'll look into the g-zip compression to perhaps merge these together to lessen the number of http requests. – Jason Weber Nov 02 '12 at 07:20

1 Answers1

1

Take a look at Cassette and render your bundles just before your closing body tag

Fisch
  • 3,775
  • 1
  • 27
  • 38
  • Thank you, @Fisch, for the link, and more importantly the knowledge! For whatever reason, I always thought javascript belonged above your closing head tag, but I changed all 5 master pages, and things are working perfectly, and I'm not getting that error message on Google's speed test thing. Thank you for taking the time to read my question and respond! – Jason Weber Nov 02 '12 at 07:15