<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 ?
<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 ?
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
?>
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>
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.
looks like your script is not registered on page load, is it inside Head tag?
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 ?