I’ve been working on this PHP CPU and RAM monitor for a while now – http://nereus.rikkuness.net/php-cpu-monitor/
And it’s all working great and does what it’s supposed to do. But I use Linux so do not have much ability to test on IE. A few friends are saying that it works on everything modern but completely crashes IE.
Just wondering if anyone had any idea as to why that might be?
Code is available here – http://php-cpu-monitor.googlecode.com/files/php-cpu-monitor-1.0.tar.gz
I’ve had some problems very similar to this one in the past, and they are usually always related to caching. And since your AJAX requests are getting cached, your PHP script is no longer sleeping for the user’s given interval, which is why you are seeing the requests complete so much faster.
Background on Caching
There are really only three response headers that your web server will issue to control how your AJAX resources are cached. These apply both to HTTP as well as AJAX requests. You don’t really need to understand these for your purposes, but it is always good information to know:
How jQuery.load() Works
jQuery.load() works by sending a simple GET request for html resources on the server. Keep in mind that the jQuery
load()function is the simplest way to fetch data, and oftentimes isn’t the best method of data retrieval. It is designed to be a quick way to grab html from the server and subsequently populate the html within your selected jQuery DOM elements. Here’s a snippet from the docs:Since
$.load()is primarily a simple way to insert html into jQuery elements, the jQuery library doesn’t provide a mechanism to ensure that your requests are not being cached. This is reasonable, because if you are simply retrieving HTML, we can get much increased performance by caching these responses, especially if they haven’t changed and are being polled frequently.Of all the browsers I tested, it looks like IE(9) is the only one that consistently caches AJAX requests. Look at the difference between the requests made in Firefox vs. Internet Explorer:
Firefox:
Internet Explorer:
Notice the suspicious <1 ms response time from IE. IE doesn’t ever refresh cached content before it’s expiration date, nor does it distinguish between HTTP and AJAX requests when it comes to caching. We need to try a different solution in order to ensure that we are receiving fresh data each time we make the call to your AJAX URL.
Preventing Internet Explorer from Caching AJAX Requests
There are a couple different ways we can workaround IE’s caching limitations:
?token=[timestamp]. By altering the request URL for each request, browsers will store different cached copies of the resource for each request, and you’ll ensure that you get the latest data from the server. jQuery creates a nonce value by simple getting a timestamp like so:var nonce = ( new Date() ).getTime();$http_response_headerarray (see here for the documentation)jQuery.ajax()instead ofjQuery.load(): With$.ajax()you can specify certain AJAX calls to force refresh using thecache: falseoption. Or even more, you can ensure that all AJAX calls are refreshed each time by configuring this in your ajaxSetup():$.ajaxSetup ({ cache: false });. This is essentially the same solution as #1, but without the need to create a nonce value in your javascript code.Of all the alternatives, #4 is the best preserves the type of request you are making and is the easiest to configure.
One other suggestion – using
window.setInterval()to call yourreplay()function is an easy way to set waiting intervals between requests, and it’s usually better to implement variable-length sleep cycles on the client than the server.