1//Style Sheet Switcher version 1.1 Oct 10th, 2006 2//Author: Dynamic Drive: http://www.dynamicdrive.com 3//Usage terms: http://www.dynamicdrive.com/notice.htm 4 5//Unofficial Update to fix Safari 5.1 glitch re: alternate stylesheets or the disabled property in regards to them 6// See: http://www.dynamicdrive.com/forums/showthread.php?p=259199 for more info 7 8var manual_or_random="manual" //"manual" or "random" 9var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random. 10 11//////No need to edit beyond here////////////// 12 13function getCookie(Name) { 14var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair 15if (document.cookie.match(re)) //if cookie found 16return document.cookie.match(re)[0].split("=")[1] //return its value 17return null 18} 19 20function setCookie(name, value, days) { 21var expireDate = new Date() 22//set "expstring" to either future or past date, to set or delete cookie, respectively 23var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5) 24document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"; 25} 26 27function deleteCookie(name){ 28setCookie(name, "moot") 29} 30 31 32function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled 33var i, cacheobj, altsheets=[""]; 34if(setStylesheet.chosen) 35try{ 36document.getElementsByTagName('head')[0].removeChild(setStylesheet.chosen); 37}catch(e){} 38for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) { 39if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title 40cacheobj.disabled = true 41altsheets.push(cacheobj) //store reference to alt stylesheets inside array 42if(cacheobj.getAttribute("title") == title){ //enable alternate stylesheet with title that matches parameter 43cacheobj.disabled = false //enable chosen style sheet 44setStylesheet.chosen = document.createElement('link');//cloneNode(false); 45setStylesheet.chosen.rel = 'stylesheet'; 46setStylesheet.chosen.type = 'text/css'; 47if(cacheobj.media) 48setStylesheet.chosen.media = cacheobj.media; 49setStylesheet.chosen.href = cacheobj.href; 50document.getElementsByTagName('head')[0].appendChild(setStylesheet.chosen); 51} 52} 53} 54if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non) 55var randomnumber=Math.floor(Math.random()*altsheets.length) 56altsheets[randomnumber].disabled=false 57} 58return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet 59} 60 61 62 63function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie 64if (document.getElementById){ 65setStylesheet(styletitle) 66setCookie("mysheet", styletitle, days) 67} 68} 69 70function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu 71if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu 72var element=(element.type=="select-one") ? element.options : element 73for (var i=0; i<element.length; i++){ 74if (element[i].value==selectedtitle){ //if match found between form element value and cookie value 75if (element[i].tagName=="OPTION") //if this is a select menu 76element[i].selected=true 77else{ //else if it's a radio button 78element[i].checked=true 79} 80break 81} 82} 83} 84} 85 86if (manual_or_random=="manual"){ //IF MANUAL MODE 87var selectedtitle=getCookie("mysheet") 88if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored 89setStylesheet(selectedtitle) 90} 91else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE 92if (randomsetting=="eachtime") 93setStylesheet("", "random") 94else if (randomsetting=="sessiononly"){ //if "sessiononly" setting 95if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty 96document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value 97else 98setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie 99} 100else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting 101if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable 102setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value 103setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable 104} 105else 106setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie 107} 108}
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.