PDA

View Full Version : Switch Content Script II, write out page contracted



Hotrod
12-14-2010, 05:57 PM
Switch Content Script II
http://www.dynamicdrive.com/dynamicindex17/switchcontent2.htm

First: Excellent script, thanks.

I'm using it to display my email, so I run thru the database and write the subjects out to a page. When done the script iterates thru the divs on the page collapsing the content.

Everything works well, when I expand the subject the script retrieves the messages from the database and contracts the previously expanded message.

My problem is when I show about 200 or more subjects the asp page writes out fast but then the script has to iterate thru all the divs collapsing and that can add a couple of seconds. I would like to write out the page with the content contracted and I'm not sure how to go about it or even if it's possible.

Can anyone help.

ddadmin
12-14-2010, 08:36 PM
Hmm I'm not sure how realistic it is to modify the script to write all the DIVs- including all of its settings such as the status icons- dynamically when the page loads, instead of having the script do that dynamically. It's possible I suppose, though quite a lot of changes to the script has to occur here, including integrating with your server side script in the backend.

You can increase the initialization time slightly by replacing the original "collectElementbyClass()" function inside switchcontent.js with the below version instead:


switchcontent.prototype.collectElementbyClass=function(classname){ //Returns an array containing DIVs with specified classname
var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i") //regular expression to screen for classname within element
this.headers=[], this.innercontents=[]
if (document.querySelectorAll)
var targetelements=document.querySelectorAll(this.filter_content_tag+'.'+classname)
else if (this.filter_content_tag!="") //If user defined limit type of element to scan for to a certain element (ie: "div" only)
var allelements=document.getElementsByTagName(this.filter_content_tag)
else //else, scan all elements on the page!
var allelements=document.all? document.all : document.getElementsByTagName("*")
var contentlength=targetelements? targetelements.length : allelements.length
for (var i=0; i<contentlength; i++){
if (targetelements || (typeof allelements[i].className=="string" && allelements[i].className.search(classnameRE)!=-1)){
var targetelement=targetelements? targetelements[i] : allelements[i]
if (document.getElementById(targetelement.id+"-title")!=null){ //if header exists for this inner content
this.headers[this.headers.length]=document.getElementById(targetelement.id+"-title") //store reference to header intended for this inner content
this.innercontents[this.innercontents.length]=targetelement //store reference to this inner content
}
}
}
}

Hotrod
12-15-2010, 12:04 PM
Thank you DDADMIN,

Wow, that small change shaved off roughly 2-3 seconds on a 9-10 second page load.

Cheers

ddadmin
12-15-2010, 11:33 PM
Glad to hear. :)