How to Enforce Download of JavaScript File

When debugging javascript file, it is necessary for browser to download the javascript file everything. However, the browser won't. Indeed it is the beauty of the technology because it saves a lot of traffic time. You can't complaint it. What you can complaint is why the browser's designer does not include a special key for user to decide when the javascript file should be downloaded.

Let us face the reality now. If your javascript debugging tool does not do it for you, there are several ways to do it. For instance, you can change javascript file name each time, you can also add version number to it, or you can even change the directory for every version. The problem here is you will also need to change the code in head section of html file. It is not fun after all.

Here is the suggestion. If your problem does not have php back end, use change file name option. Put a version number directly after the name, like: jsname01.js, janame02.js, jsname03.js, etc.

If you have php back end, you can wrap your js file by php. Here is the method:

1. Change your jsname.js file to jsname.js.php.

2. At the beginning of the file, add:

<?php
print
<<<NOWDOC

At the end of the file, add:

NOWDOC;
?>

3. Change all your javascript open comments from // to <!-- ... -->. It would cause the trouble for open comments in .js.php file. For more information on javascript comments, please refer to Code Conventions: JavaScript.

4. Change your html head to: <script language='Javascript' src='filename.js.php' type='text/javascript'></script>.

5. When ready to deploy, change all these back.

I call this PHP Wrapping JavaScript Debugging Method.

No comments:

Post a Comment

Labels