Showing posts with label Enable PHP. Show all posts
Showing posts with label Enable PHP. Show all posts

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.

Error Message: XML cannot be the whole program

Error message "XML cannot be the whole program" appears when load a javascript file which was generated by PHP file. It actually has nothing to do with its appearance. There are two possible causes:

1. The file contains <script> and </script>. These two scripts indeed are unnecessary because in your html file, it had been already included: <script language='Javascript' src='filename.js.php' type='text/javascript'></script>.

2. Your php code is not clean. It contains some form of invisiable editorial codes inserted by your editor, such as Notepad, etc.

What Happened If Your PHP Not Showing?

If PHP was not enabled, your .php file will be treated as .html file. So, <?php…> will be treated as html tag, which would not show at all. More tricky is, if your codes include ->, the first > will be treated as the end tag for <?php…>. This would confuse you because you would think the first -> has been mistakenly regarded as ?>.

Here are solutions. First, save as following piece of codes as phpinfo.php:

<?php
phpinfo();
?>

Then call it in your browser. It will list all of the configuration information about php. If it shows a blank page, it means your php has not been enabled.

If you are in personal server, run following scripts:

LoadModule php5_module "/path/to/php5apache2_2.dll" #use php5apache2.dll for apache 2.x, use php5apache.dll for apache1.x

AddType application/x-httpd-php .php

If you are in shared server, contact administrator.

The another possibility is your server does not recognize your php codes if they were composited by text editor such as Notepad, which indeed still have some uncleared characters. To overcome this, find a pure cleaning php code editor, such as Crimson Editor, copy to it and save as new .php file.

Labels