Prevent caching of dynamically added iframes

I wanted to insert an iframe in a webpage, chosen at random among the files panel_1.html, panel_2.html, panel_3.html.

I first tried to setup the iframe with JavaScript at onload with innerHTML or with document.write in a div container called “content”. An example is shown herafter:

var i = 1 + Math.round(Math.random()*(2));

document.getElementById(‘content’).innerHTML='<iframe src=”panel_’ +i +’.html” width=”100%” height=”100%”></iframe>’;

These solutions worked as expected in the Google Chrome browser, but there were problems either with the Firefox browser or with the IE browser due to caching problems. The iframe was not refreshed despite changing the src data in the iframe tag.

Most proposals found on the web with a Google search did’nt solve the problem on all browsers. By setting the src of the Iframe instead of changing the innerHTML of its container as shown hereafter the iframes are refreshed in the 3 browser’s IE, Firefox and Chrome.

<iframe id=”mypanel” src=”about:blank” width=”100%”
height=”100%” ></iframe>
<script type=”text/javascript”>
var i = 1 + Math.round(Math.random()*(2));
document.getElementById(‘mypanel’).src=’panel_’+i+’.html’
</script>

I would like to add that by using the <object> tag insted of the <iframe> tag, cross-browser problems are even worse.

Julia applet, object, classid, codebase, …

The tags “embed” and “applet” are deprecated and even dropped in the new browser standards like XHTML 1.1. To embed objects within a document, the tag “object” should be used. The main attributes of “object” are:

  • classid : This attribute contains a URL for an object’s implementation. The URL syntax depends upon the object’s type. With ActiveX controls (Internet Explorer), the value of this attribute is not a URL, but an object-id with the prefix clsid. The way to access a Java applet is to use classid=”java: classname.class”. The pseudo-URL java: is used to indicate a Java applet.
  • data : This attribute contains a URL for data required by an object.
  • archive : This attribute contains a URL for the location of an archive file. An archive file typically is used to contain multiple object files to improve the efficiency of access.
  • codebase : This attribute contains a URL to use as a relative base to access the object specified by the classid attribute.
  • codetype : This attribute specifies an object’s MIME type.
  • type : This attribute specifies the MIME type for the object’s data.
  • declare : This attribute declares an object without instantiating it. This is useful when the object will be a parameter to another object.
  • name : Under the Microsoft definition, this attribute defines the name of the control so scripting can access it.
  • standby : This attribute contains a text message to be displayed while the object is loading.
  • width, height : the size of the object

The tag “param” specifies the parameters that are needed by the object to run. Most attributes exist also as parameter elements, but should never be used duplicated, except for the codebase attribute. In the “object” tag, the codebase attribute represents the location from which to download the object (for instance the Java plugin) when it is not found on the local machine. In the “applet” tag, the codebase attribute represents the location from which to download additional class and jar files. To resolve this conflict, the codebase attribute is mapped into a “param” codebase in the “object” tag.

Specific parameters are the following:

  • scriptable : Specifies whether the applet is scriptable from the HTML page using JavaScript or VBScript. The value can be either “true” or “false”.
  • mayscript : Specifies whether the applet is allowed to access “netscape.javascript.JSObject”. The value can be either “true” or “false”.
  • code : Specifies the name of the Java applet or JavaBeans component witout the extension “class”. It cannot be used with “param” element “object” nested inside the same “object” tag.

Microsoft Internet Explorer handles the “object” tag differently than other browser like Firefox, mainly due to the different implementation of the “classid” attribute. To provide multiple browser support, it is necessary to program a conditional settings of the “object”  tag by nesting the tags for IE and for other browsers. An example is shown below with the tiny Julia applet (128 bytes)  created by the russian programmer Konstantin Victorovich Morshnev (alias MoKo).

A detailed tutorial about (Multiple Browser Supported) Java applet using XHTML ‘object’ tag is given by Shayne Steele. A user guide to embed applets in documents with the “object” tag is published by Sun.