Forum Moderators: open

Message Too Old, No Replies

JavaScript PHP IE help

         

andrewsmd

7:22 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a weird IE problem. I have a php web page that has internal JavaScript within it. I have a JavaScript function like so
function loadFile(targetFile)
{
var elem = document.createElement("script");
elem.setAttribute("src", targetFile);
document.getElementsByTagName("body")[0].appendChild(elem);
}
I have checkboxes that use this function to call and external php file when they are clicked, example:
<td class = "checkBox"><input type="checkbox" name="highmark" onclick="loadFile('highmark.php')"></td>
assume highmark.php is a valid php file that outputs a message on an alert box if a file exists otherwise it sets inner.html to a hidden div. If the file exists, the alert box works in both IE and Firefox, however if then a few minutes later the file doesn't exist anymore, IE still displays the alert box on a button click. Firefox works the moment the file does not exist. If you wait for like 10 or 15 minutes then IE responds the way it should. Does IE keep some sort of internal session or something like that? Thanks,

londrum

7:43 pm on Jul 2, 2008 (gmt 0)

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



maybe it's just a caching thing.
try putting a random number on the end of the url every time, something like highmark.php?43534534, and see if it still does it

andrewsmd

8:28 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do I append a random number to the end of the url? I know how to generate one with php, but how do I add it. Thanks

londrum

9:08 pm on Jul 2, 2008 (gmt 0)

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



if you name your random number '$random', for example, then you can just put this in your code...

<input type="checkbox" name="highmark" onclick="loadFile('highmark.php?<?php echo $random; ?>')">

it's only really to test whether its a caching problem though. if it is a caching problem then you can either leave it like that, or put some php headers on the highmark.php page to stop it being cached.

andrewsmd

9:38 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What would those php headers be because I cannot just echo a random number like that because I have a lot of calls to these functions. And inserting that much php in my index page is not an option.

eelixduppy

9:42 pm on Jul 2, 2008 (gmt 0)



Taken from the documentation:

[url=http://www.php.net/header]header[/url]("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Just note these headers must come before any content is output to the screen. This includes HTML as well as white space.

rocknbil

1:58 am on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree, it's very possibly caching. IF the headers fix it, good, otherwise it's pretty easy to generate a unique number, although this one's not random:


function loadFile(targetFile) {
var day = new Date();
var id = day.getTime();
targetFile += '?'+id;
var elem = document.createElement("script");
elem.setAttribute("src", targetFile);
document.getElementsByTagName("body")[0].appendChild(elem);
}

andrewsmd

12:44 pm on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to let you know, the headers didn't work. I don't actually ever output any html with PHP so they could go anywhere. I don't know if it's the code or if it's because I already have a header Header("content-type: application/x-javascript"); Whenever I output anything it is either
$message = "some message";
echo "alert('$message')";
or
$name = "nameOfEmptyDiv";
$message = "some message";
echo("document.getElementById('".$name."').innerHTML = '".$message."'");
I still put them before the echo's however.
The JavaScript edit worked just fine thanks. Just out of curiosity, how does it actually work. From what I see it gets the time and time stamps my index file, but where? Thanks,

Fotiman

4:24 pm on Jul 3, 2008 (gmt 0)

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



The JavaScript is simply getting a new Date (which will be the current date/time), and getting the time from that, and then appending that value to the end of your targetFile. So if you passed in a targetFile value of "foo.php", this will change that to "foo.php?1215102081905" (replacing that number with whatever the getTime() method returns).

The JavaScript could be simplified a little to this:


function loadFile(targetFile) {
targetFile += '?ck=' + (new Date()).getTime();
var elem = document.createElement("script");
elem.setAttribute("src", targetFile);
document.getElementsByTagName("body")[0].appendChild(elem);
}

Note, I eliminated variables that were not needed (only used once) and I also added "ck=" before the value (ck stands for cache killer). I prefer not to pass numeric values without passing them as a key/value pair... personal preference. :)

[edited by: Fotiman at 4:26 pm (utc) on July 3, 2008]

andrewsmd

5:07 pm on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see, I was wondering because it wasn't appending that to the end of my index.php file. Thanks for all of your help