tags:

views:

137

answers:

6
<script type="text/javascript" src="test.php"></script>

The first time I visit that page,test.php is not visited,

but if I refresh the page,it'll get run.

Have you ever experienced this ?

+9  A: 

It will happen when test.php is cached locally or remotely.

Tor Valamo
if it's just that, then shouldn't test.php be visited the first time and exactly not when it is refreshed?
catchmeifyoutry
it'll be cached from the *last* time you refreshed.
nickf
exactly, but the question states that it's not loaded during the _first_ time, so how could it be cached?
catchmeifyoutry
I'm guessing he doesn't take into account that other sessions may cache it for him.
Tor Valamo
+2  A: 

This might happen when the test.php file is cached -- which can be either in the browser's cache, or by some proxy allong the way.


Forbidding caching of test.php might help ; this can be done sending the right HTTP headers -- quoting an example from the header manual page :

PHP scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
Pascal MARTIN
+2  A: 

Cache is likely causing the interruption in calls. You can force this to be called each visit by appending a constantly-changing string to the path:

<script type="text/javascript" src="test.php?u=<?php print time(); ?>"></script>
Jonathan Sampson
+4  A: 

As Tor and Silky said, it's most likely caused by the page being cached. If you have Firebug installed, open the "Net" tab when you go to the page and you might see it say "304 Not Modified" - this means it has been cached.

The easiest and most reliable way (in my experience) to stop a file being cached is to add a random query string to it:

<script type="text/javascript" src="test.php?_=46031535"></script>

You'll just have to change it on each page load (either on the server or client side).

Of course, if the caching headers are set up properly on the server, then this shouldn't be an issue, but I've always found this method to be easier.

nickf
Following on from this; it's actually good (if your system supports it) to put the revision number of the file on it (SO does this; and I do it automatically via some build scripts, for my static files). This way, the same version is always cached, but new versions are redownloaded immediately.
Noon Silk
+2  A: 

looks like your script is not registered on page load, is it inside Head tag?

Ali
A: 

How do you know it is not visited ?

Do you assume it, because some code inside it does not execute ?

If that is the case, it might be that you are accessing the DOM of the page while the page has not yet completely loaded .. (and thus the javascript fails)

But the second time (and thereafter) the html of the page is cached and so when the script tries to access the DOM it is already there and the code works..

just a different thought ;)

ps. do you get any Javascript errors ?

Gaby