Symbian developer community

 
wiki

Avoid Problems with Browser Cache in WRT Widgets

From Symbian Developer Community

Jump to: navigation, search
Recipe
Amount of time required: 10 minutes
Required libraries: N/A
Required header files: N/A
PlatSec Capabilities: N/A
Compatibility: Tested with S60 3rd Edition FP2 and S60 5th Edition v1.0 SDKs.
Comes with Code: Embedded in this page

Problem: Browser cache may interfere when retrieving content over HTTP in WRT widgets

Solution: Append unique parameters to the URL

Discussion: Whenever we use widget.openURL(...) or make XmlHttpRequests, browser cache may decide to deliver cached version of the requested content. This is great in some situations, but in others it may be a problem.

We can use a simple trick in order to prevent cache from interfering with the HTTP request - appending unique parameter to the request URL. Browser will see a new, unique URL that does not match cached ones and send the request through to the server.

Here is a function that takes original URL and appends parameter as discussed:

 
function nocache(url) {
if (url.indexOf("?") == -1) {
url += "?";
} else {
url += "&";
}
url += "nocache=" + (new Date().getTime());
return url;
}
 

Using this function is simple. Here is an example using WRTKit ajax call:

 
var url = "http://myhost.com/myajax.call?oid=123";
var httpReq = new Ajax();
httpReq.onreadystatechange = readyStateChanged;
httpReq.open("GET", nocache(url), true);
httpReq.send(null);
 

Here is an example when opening URL in a new browser window:

 
var url = "http://myhost.com/mypage.html";
if (window.widget) {
widget.openURL(nocache(url));
}
 

Comments

Sign in to comment…