From: Jordi 'I. M. <jmi...@or...> - 2000-12-13 11:44:56
|
Here's something that had been running around my head for a while. When doing big applications it is common to have a hidden data frame where we submit data or retrieve data from into our web application. One common caveat is to detect that data frame's onLoad events. Typically we would place some code in that frame's source so it informs the caller of its loading completion and so and so on. Another annoying situation is having a frame structure and performing some initialization stuf each time content in one frame is changed ( creating a floating menu inside that page, for example ) This object solves this problem by managing loads of its target frame and capturing onloads events0. This way the loaded page itsef does not have to contain any synchronizing code. Here goes the code and attached is a simple example. I could have upped it via CVS but I'm not sure about where does it fit or if it should be considered part of the API. Maybe we need some sort of a 'misc. scrips' folder. /* Frame Load manager: allows to cast external file loads into a frame and program their onload events. IlMaestro (ilm...@ca...) 2000.13.12 Usage: mr = new loadManager(myframe) mr.load("p.html") */ loadManager = function(frame) { this.id = "LM"+(loadManager.Count++) window[this.id] = this this.frame = frame /* Should we catch links within the content, so when they are clicked the new page is loaded via our object as well, allowing us to detect the new page's onLoad event ? */ this.catchLinks = true this.onload = new Function() } /* Use this method to load urls into the target frame */ loadManager.prototype.load = function(url) { with(this.frame.document) { /* It seems that NS has troubles with one-frame framesets, therefore we resort to a dummy frame that is anyway invisible */ open() write("<frameset onLoad='window.pointerToLoader.loaded()' cols='110%,*' border=0>") write("<frame name='"+this.id+"Content' src='"+url+"' border=0>") write("<frame name='"+this.id+"Dummy' src='about:blank' border=0 scrolling=no>") write("</frameset>") close() } this.frame['pointerToLoader']=this } /* This method is called by the code we wrote into the target frame once the frameset is loaded ( which means that the content frame has finished loading ) */ loadManager.prototype.loaded = function() { if(this.catchLinks) this.parseLinks() this.onload() } /* This method parses the links in the target document and replaces their content with calls to our loader. There are a number of cases to be taken into consideraration: for example, links in the form of 'javascript:...' must be left untouched. I bet I'll miss some cases :( */ loadManager.prototype.parseLinks = function() { eval("var e=this.frame."+this.id+"Content.document.links") for (var i=0;i<e.length;i++) { l = e[i].href if(l.substring(0,11)!='javascript:' && l.substring(0,7)!='mailto:') e[i].href = 'javascript:parent.pointerToLoader.load("'+l+'")' } } loadManager.Count=0 |