To load an doc inside a iframe dynamically

We can load a document into an iframe dynamically by using iframe.src = “yourpage” in javascript.
But this works well in IE and not in others browsers. Here is an method to load iframes dynamically.

I found this at http://developer.apple.com/internet/webcontent/iframe.html and just changed something to fit our need.


var IFrameObj;
function loadIFrame(thePage){
//loads an doc inside a iframe dynamically
URL = thePage;
IFrameObj = new Object();
IFrameObj.document = new Object();
IFrameObj.document.location = new Object();
IFrameObj.document.location.iframe = document.getElementById('cpaneif');
IFrameObj.document.location.replace = function(location) {
this.iframe.src = location;
}

if (navigator.userAgent.indexOf(‘Gecko’) !=-1 && !IFrameObj.contentDocument) {
// allow a fraction of a second to recognize the new IFrame
//setTimeout(‘loadIFrame(“‘+thePage+'”)’,10);
//return false;
}

if (IFrameObj.contentDocument) {
// For NS6
IFrameDoc = IFrameObj.contentDocument;
} else if (IFrameObj.contentWindow) {
// For IE5.5 and IE6
IFrameDoc = IFrameObj.contentWindow.document;
} else if (IFrameObj.document) {
// For IE5
IFrameDoc = IFrameObj.document;
} else {
return true;
}

IFrameDoc.location.replace(URL);
/* NB:-
//if you want to create a new iframe dynamically visit
http://developer.apple.com/internet/webcontent/iframe.html
*/
}

One Reply to “To load an doc inside a iframe dynamically”

  1. i am having problems with this script.
    it seems that location.iframe and location.replace
    fail in IE7. have you run into problems running
    this script (specifically with the properties mentioned)?

    thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *