1//** Ajax Tabs Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com) 2//** Updated Oct 21st, 07 to version 2.0. Contains numerous improvements 3//** Updated Feb 18th, 08 to version 2.1: Adds a public "tabinstance.cycleit(dir)" method to cycle forward or backward between tabs dynamically. Only .js file changed from v2.0. 4//** Updated April 8th, 08 to version 2.2: 5// -Adds support for expanding a tab using a URL parameter (ie: http://mysite.com/tabcontent.htm?tabinterfaceid=0) 6// -Modified Ajax routine so testing the script out locally in IE7 now works 7 8var ddajaxtabssettings={} 9ddajaxtabssettings.bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no) 10ddajaxtabssettings.loadstatustext="<img src='/images/loading.gif' /> Requesting content..." 11 12 13////NO NEED TO EDIT BELOW//////////////////////// 14 15function ddajaxtabs(tabinterfaceid, contentdivid){ 16 this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container 17 this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container 18 this.enabletabpersistence=true 19 this.hottabspositions=[] //Array to store position of tabs that have a "rel" attr defined, relative to all tab links, within container 20 this.currentTabIndex=0 //Index of currently selected hot tab (tab with sub content) within hottabspositions[] array 21 this.contentdivid=contentdivid 22 this.defaultHTML="" 23 this.defaultIframe='<iframe src="about:blank" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" class="tabcontentiframe" style="width:100%; height:auto; min-height: 200px"></iframe>' 24 this.defaultIframe=this.defaultIframe.replace(/<iframe/i, '<iframe name="'+"_ddajaxtabsiframe-"+contentdivid+'" ') 25this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values) 26 this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link") 27} 28 29ddajaxtabs.connect=function(pageurl, tabinstance){ 30 var page_request = false 31 var bustcacheparameter="" 32 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) 33 try { 34 page_request = new ActiveXObject("Msxml2.XMLHTTP") 35 } 36 catch (e){ 37 try{ 38 page_request = new ActiveXObject("Microsoft.XMLHTTP") 39 } 40 catch (e){} 41 } 42 } 43 else if (window.XMLHttpRequest) // if Mozilla, Safari etc 44 page_request = new XMLHttpRequest() 45 else 46 return false 47 var ajaxfriendlyurl=pageurl.replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/") 48 page_request.onreadystatechange=function(){ddajaxtabs.loadpage(page_request, pageurl, tabinstance)} 49 if (ddajaxtabssettings.bustcachevar) //if bust caching of external page 50 bustcacheparameter=(ajaxfriendlyurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime() 51 page_request.open('GET', ajaxfriendlyurl+bustcacheparameter, true) 52 page_request.send(null) 53} 54 55ddajaxtabs.loadpage=function(page_request, pageurl, tabinstance){ 56 var divId=tabinstance.contentdivid 57 document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message" 58 if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){ 59 document.getElementById(divId).innerHTML=page_request.responseText 60 ddajaxtabs.ajaxpageloadaction(pageurl, tabinstance) 61 } 62} 63 64ddajaxtabs.ajaxpageloadaction=function(pageurl, tabinstance){ 65 tabinstance.onajaxpageload(pageurl) //call user customized onajaxpageload() function when an ajax page is fetched/ loaded 66} 67 68ddajaxtabs.getCookie=function(Name){ 69 var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair 70 if (document.cookie.match(re)) //if cookie found 71 return document.cookie.match(re)[0].split("=")[1] //return its value 72 return "" 73} 74 75ddajaxtabs.setCookie=function(name, value){ 76 document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/) 77} 78 79ddajaxtabs.prototype={ 80 81 expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers 82 this.cancelautorun() //stop auto cycling of tabs (if running) 83 var tabref="" 84 try{
85 if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr 86 tabref=document.getElementById(tabid_or_position) 87 else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr 88 tabref=this.tabs[tabid_or_position] 89 } 90 catch(err){alert("Invalid Tab ID or position entered!")} 91 if (tabref!="") //if a valid tab is found based on function parameter 92 this.expandtab(tabref) //expand this tab 93 }, 94 95 cycleit:function(dir, autorun){ //PUBLIC function to move foward or backwards through each hot tab (tabinstance.cycleit('foward/back') ) 96 if (dir=="next"){ 97 var currentTabIndex=(this.currentTabIndex<this.hottabspositions.length-1)? this.currentTabIndex+1 : 0 98 } 99 else if (dir=="prev"){ 100 var currentTabIndex=(this.currentTabIndex>0)? this.currentTabIndex-1 : this.hottabspositions.length-1 101 } 102 if (typeof autorun=="undefined") //if cycleit() is being called by user, versus autorun() function 103 this.cancelautorun() //stop auto cycling of tabs (if running) 104 this.expandtab(this.tabs[this.hottabspositions[currentTabIndex]]) 105 }, 106 107 setpersist:function(bool){ //PUBLIC function to toggle persistence feature 108 this.enabletabpersistence=bool 109 }, 110 111 loadajaxpage:function(pageurl){ //PUBLIC function to fetch a page via Ajax and display it within the Tab Content instance's container 112 ddajaxtabs.connect(pageurl, this) 113 }, 114 115 loadiframepage:function(pageurl){ //PUBLIC function to fetch a page and load it into the IFRAME of the Tab Content instance's container 116 this.iframedisplay(pageurl, this.contentdivid) 117 }, 118 119 setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link") 120 this.selectedClassTarget=objstr || "link" 121 }, 122 123 getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to 124 return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref 125 }, 126 127 urlparamselect:function(tabinterfaceid){ 128 var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL 129 return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index 130 }, 131 132 onajaxpageload:function(pageurl){ //PUBLIC Event handler that can invoke custom code whenever an Ajax page has been fetched and displayed 133 //do nothing by default 134 }, 135 136 expandtab:function(tabref){ 137 var relattrvalue=tabref.getAttribute("rel") 138 //Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easy searching through 139 var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : "" 140 if (relattrvalue=="#default") 141 document.getElementById(this.contentdivid).innerHTML=this.defaultHTML 142 else if (relattrvalue=="#iframe") 143 this.iframedisplay(tabref.getAttribute("href"), this.contentdivid) 144 else 145 ddajaxtabs.connect(tabref.getAttribute("href"), this) 146 this.expandrevcontent(associatedrevids) 147 for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected" 148 this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("href")==tabref.getAttribute("href"))? "selected" : "" 149 } 150 if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers 151 ddajaxtabs.setCookie(this.tabinterfaceid, tabref.tabposition) 152 this.setcurrenttabindex(tabref.tabposition) //remember position of selected tab within hottabspositions[] array 153 }, 154 155 iframedisplay:function(pageurl, contentdivid){ 156 if (typeof window.frames["_ddajaxtabsiframe-"+contentdivid]!="undefined"){ 157 try{delete window.frames["_ddajaxtabsiframe-"+contentdivid]} //delete iframe within Tab content container if it exists (due to bug in Firefox) 158 catch(err){} 159 } 160 document.getElementById(contentdivid).innerHTML=this.defaultIframe 161 window.frames["_ddajaxtabsiframe-"+contentdivid].location.replace(pageurl) //load desired page into iframe 162 }, 163 164 165 expandrevcontent:function(associatedrevids){
166 var allrevids=this.revcontentids 167 for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface 168 //if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it 169 document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none" 170 } 171 }, 172 173 setcurrenttabindex:function(tabposition){ //store current position of tab (within hottabspositions[] array) 174 for (var i=0; i<this.hottabspositions.length; i++){ 175 if (tabposition==this.hottabspositions[i]){ 176 this.currentTabIndex=i 177 break 178 } 179 } 180 }, 181 182 autorun:function(){ //function to auto cycle through and select tabs based on a set interval 183 this.cycleit('next', true) 184 }, 185 186 cancelautorun:function(){ 187 if (typeof this.autoruntimer!="undefined") 188 clearInterval(this.autoruntimer) 189 }, 190 191 init:function(automodeperiod){ 192 var persistedtab=ddajaxtabs.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled) 193 var selectedtab=-1 //Currently selected tab index (-1 meaning none) 194 var selectedtabfromurl=this.urlparamselect(this.tabinterfaceid) //returns null or index from: tabcontent.htm?tabinterfaceid=index 195 this.automodeperiod=automodeperiod || 0 196 this.defaultHTML=document.getElementById(this.contentdivid).innerHTML 197 for (var i=0; i<this.tabs.length; i++){ 198 this.tabs[i].tabposition=i //remember position of tab relative to its peers 199 if (this.tabs[i].getAttribute("rel")){ 200 var tabinstance=this 201 this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers 202 this.tabs[i].onclick=function(){ 203 tabinstance.expandtab(this) 204 tabinstance.cancelautorun() //stop auto cycling of tabs (if running) 205 return false 206 } 207 if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element 208 this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/)) 209 } 210 if (selectedtabfromurl==i || this.enabletabpersistence && selectedtab==-1 && parseInt(persistedtab)==i || !this.enabletabpersistence && selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){ 211 selectedtab=i //Selected tab index, if found 212 } 213 } 214 } //END for loop 215 if (selectedtab!=-1) //if a valid default selected tab index is found 216 this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class) 217 else //if no valid default selected index found 218 this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr 219 if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){ 220 this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod) 221 } 222 } //END int() function 223 224} //END Prototype assignment
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.