Forum Moderators: open

Message Too Old, No Replies

Is PHP in an external .js file okay?

Trouble getting my JS timer script to work from a .js file.

         

bsbarker

1:01 am on May 14, 2010 (gmt 0)

10+ Year Member



I've created a JS timer and integrated it into my index.php successfully. However, after moving the script into an external .js file, the timer is no longer recognizing the numbers in the timer (it reads "NaN:NaN:NaN").

I'm using '<?php print date("F d, Y H:i:s", time())?>' to bring in the server time and my guess is that php dosn't like being in an external .js file. Does anyone know if that's my issue and have alternative suggestions?

If not I can post some code and see if anyone has any other ideas what might be wrong.

Thanks for having a look!

daveVk

3:03 am on May 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The server only processes php in .php ( or similar ) files, changing the script file extension from .js to .php should work. Be aware the script file may me cached by the client, resulting in prior time being displayed. If the current time as held by the client is Ok use the js date function.

bsbarker

3:34 am on May 14, 2010 (gmt 0)

10+ Year Member



I didn't realize I could use a .php file as the source of an external script. Thanks for the tip, it works perfectly now. I love the easy answers!

Readie

11:09 am on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As was stated above, it's possible a user could cache the .js, so it may be worth declaring the following in the PHP file:

header('Cache-Control: no-cache, must-revalidate');

TheMadScientist

4:27 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could also parse .js as .php with AddHandler and AddType settings in an .htaccess file on Apache, or use mod_rewrite and serve the info to the .js url from a .php file if you don't want people to know it's php...

<script scr="/the-file.js" type="text/javascript">

### .htaccess

RewriteEngine on
RewriteRule ^the-file\.js /the-file.php [L]

Readie

4:36 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^the-file\.js

Should have a dollar on the end there:

RewriteRule ^the-file\.js$ /the-file.php [L]

bsbarker

4:04 am on May 17, 2010 (gmt 0)

10+ Year Member



Thanks for the suggestions, guys. I definitely don't want the file to be cached. I added the header() line you suggested to the top of the PHP file, but it gave me an "object expected" error. I'm still pretty new to web programming, can you let me know how to implement this correctly?

Also,
### .htaccess

RewriteEngine on
RewriteRule ^the-file\.js$ /the-file.php [L]

Do I add all three lines to my .htaccess file or just the last two?

TheMadScientist

4:29 am on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just these two:

RewriteEngine on
RewriteRule ^the-file\.js$ /the-file.php [L]

And yeah, the little line ender is a good idea for an exact match where you don't want to send the info from /the-file.php to requests for /the-file.js3. Thanks for pointing out the little technicality there Readie.

Just so you know what it does bsbarker:
^ = the beginning of a line.

the-file = the full path to the file without the preceding / in the .htaccess file (IOW: /the-file in the .htacces won't match because if looks forward -> and you're already at /, but in the httpd.conf you'll need it.). If the file is in a directory, simply add the directory name before the file name EG if the file is at: http://www.example.com/js/main-script.js you would use: js/main-script\.js

\. = a literal dot, because in a regular expression . is 'anything except the end of a line' and that's not what we want to match here, you want it to be a literal dot, so the-file9js doesn't not serve the file too.

js = the file extension (obviously)
$ = the end of the matching pattern.

/the-file.php can be any file anywhere on your server.

[L] = LAST this tells the rewrite engine to stop processing and serve the info. A L flag should always be used unless you know you don't need it.

And that's concludes your lesson on mod_rewrite for the day.
Have fun with it. :)

Readie

12:39 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I added the header() line you suggested to the top of the PHP file, but it gave me an "object expected" error. I'm still pretty new to web programming, can you let me know how to implement this correctly?

That sounds like a JS error to me, the header() should be inside the <?php ?> tags, before anything has been printed to the document (that includes anything outside of the PHP tags)

bsbarker

5:30 am on May 18, 2010 (gmt 0)

10+ Year Member



Ah yes, you're right, Readie. I didn't have the header() within the php tags. Thanks for that.

TheMadScientist, I appreciate the explanation of the mod_rewrite. I always like to understand what my code is doing, so that was helpful.