views:

67

answers:

3

Hello, I was just wondering what the simplest javascript function would be to request a server side .js file. Currently I have a jquery-1.4.2.min file that weighs in at 70kb, and I figured that there has to be a way, using javascript, to request this file. That way, if the user doesn't have javascript enabled the function would be ignored and the jquery file wouldn't have to be downloaded, thus speeding up the download of the page and decreasing the bandwidth used by the server.

Also if this works, would the file just be downloaded, or would the page begin to use it? Thanks in advance!

+4  A: 

Most browsers already don't download JavaScript when it's disabled, so this is an over-optimization for most browser users. If I can find the question on it I'll update this...but it's something you don't need to handle :)

Edit: Here's that question, though I think there's another similar one as well.

Something else to keep in mind is that the user will only download it once if your cache headers are set correctly. Also take a look at using a CDN for your jQuery include.

Nick Craver
Awesome, thanks for the response! I had no clue that this was already handled by most browsers. I was already planning on setting the cache headers (and came across CDN), but thanks for the heads up anyway! Take care.
@user435460 - Welcome :), and welcome to SO! Be sure to accept an answer if it resolves your question via the check-mark on the left :)
Nick Craver
A: 

If the user doesn't have javascript enabled, <script> elements with src attributes will be ignored.

kennebec
A: 

If you really want to, you can document.write() the script tag or create a script element and append it. If js is disabled it will never happen. But others have mentioned already, for most modern browsers, the script tag will simply be ignored if js is disabled, so it's overkill.

Crayon Violent