0

First of all: I'm not talking about where each is executed, but what the differences in browser protocols is for fetching the script from the server (or so, I really have no clue).

I'm seeing a very strange behaviour that I can't explain and I can't find a reason for after a few days of searching on the internet.

The situation is as follows: I have some javscript that was originally stored in a .php file because there was some server side magic to be done upon fetching the script. It was called by the browser like this:

<script language="JavaScript" type="text/javascript" src="http://myserver.com/script.php"></script>

The javascript itself makes a few AJAX calls when loaded. In the (PHP) scripts activated by the AJAX calls I had a counter to keep track of traffic.

Now at some point the php magic done by script.php was removed, and I was able to simply turn the .php file into a .js like so:

<script language="JavaScript" type="text/javascript" src="http://myserver.com/script.js"></script>

It has exactly the same output, and makes exactly the same AJAX calls.

Here's the weird part: with the script.js my counter says there's 1/10th of the traffic there is with script.php. I can not figure out why it does this...

I checked the access.log of my server, and indeed, script.js gets far fewer requests than script.php did. When I revert to script.php the traffic instantly increases to 10/10th.

The main problem I have in debugging is that I can not reproduce the script not loading. I'm always in that 10% that does get the script to load/execute.

I tried to not let the script.js cache but that didn't help. I also tried watching if this behaviour was browser independant, but for me it works in FF, Chrome and IE. Furthermore, the useragent strings in the accesslogs aren't very helpfull on telling which browser it actually is.

If anyone could shine a light upon this, I'd be very thankfull.

[EDIT] Even if the js script comes from the cache, which is fine because it saves me on traffic bigtime, shouldn't it still be executed and make the AJAX calls? This doesn't happen either (Hence only 1/10th of traffic as shown by the counter)

[EDIT2] I just noticed that the script.php has Content-Type: application/javascript while the script.js has Content-Type: application/x-javascript Now I don't know if that could possibly cause this behaviour but I'll try to change it. Changing it, and changing script.js along with a newline so it needs a refresh didn't help...

7
  • what did the php file contain? some filter that suppressed loading the js everytime? Commented Jul 9, 2013 at 10:25
  • Was script.php ever requested another way? Or only ever using that particular <script> tag? Commented Jul 9, 2013 at 10:26
  • U can prevent the caching by adding myscript.js<?=now?> to the src tag Commented Jul 9, 2013 at 10:30
  • @Royal Bg: At first the php file only echo'd the javascript. Later I added cache control. It worked fine in both cases Jon: it was never requested in another way DarkBee: The point is to cache it eventually, it saves me on traffic bigtime Commented Jul 9, 2013 at 10:31
  • Unrelated, but the language attribute has long been deprecated, so don't use it Commented Jul 9, 2013 at 10:45

2 Answers 2

1

This is a tricky question to answer without being able to look into request traces. Odds are that your web server doesn't issue the same caching headers for one and the other.

When you call a PHP script, the HTTP response is generated by PHP itself and unless explicitly done, no caching headers are issued. On the opposite when you load a JS file, or any static file, it will be served directly by your HTTP server (Apache/nginx/...) and headers such as Date / If-Modified-Since or Etag headers will be issued by the web server to ease the load and avoid multiple fetches for a same version of the file.

You may want to look into these headers to ensure this is what is happening.

A common trick to make the script load each and every time (I'm not sure this is what you need) is to append a random parameter on the URL, like this :

<script language="JavaScript" type="text/javascript" src="http://myserver.com/script.js?r=<?= rand() ?>"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

Just to be complete for people who may not have noticed: this answer is not really related the problem too. thibault remarked that as comment to the other answer here.
0

Very likely this is due to different cache settings. How long the browser keeps the script in the cache before ever asking the server for it again is determined by HTTP cache headers. For PHP scripts the web server is likely configured to output headers that cause the browser to not cache it at all and request it again every single time. For .js files the defaults are likely to cache it for a while, since .js files don't typically change that often.

3 Comments

Well like I said, I tried adding a "no cache" to the script.js, and that worked, it actually gave me a 200 for the script.js every time I loaded it, but then the counter in the AJAX script should show normal traffic which doesn't happen. Also: even though the script is fetched from the cache, it should still EXECUTE right? Which doesn't happen 9/10 times
Just saw your comment, my answer is off too, and I have no clue right now !
@thibauts: that's too bad =/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.