Hi all, I am a bit stuck and looking for a way to do the same : http://www.thesitewizard.com/javascr...e-sheets.shtml but not using cookies...instead using local storage... It is a bit new to me.....any help would be great Many Thanks
Forum and/or your copy/paste mangled your URL, though I assume you mean change-style-sheets.shtml converting it to local storage should be as simple as replacing the set_cookie and get_cookie function with the much simpler localStorage array. var localStorageStyleName = "demo theme" ; function switchStyle ( styleTitle ) { var linkList = document.getElementsByTagName("link"); for ( var i=0; i<linkList.length; i++ ) { if ( (linkList[i].rel.indexOf('stylesheet') != -1) && (linkList[i].title) ) { linkList[i].disabled = !(linkList[i].title == styleTitle); } localStorage[localStorageStyleName] = styleTitle; } } function onloadHandler() { if (localStorage[localStorageStyleName]) { switchStyle( localStorage[localStorageStyleName] ); } } Code (markup): I think should do it. Just remember you might want to provide cookie support as a fallback, and naturally don't forget to include scripting off fallbacks to handle/track this server side, at which point why throw javascript at this in the first place? Side note, always laugh at the outright bad coding in most such tutorials. Extra variables and functions for nothing, late var creation, unclear var creation, naming conventions that don't match the languages -- suppose it goes hand in hand with throwing javascript at something that really has little if any business being handled client-side.
Hi death Shadow...Thanks soo much for your help.....it is working like a charm...! I am so grateful . Just one more thing if you dnt mind....how do i put cookie as a fall back machanism ? (Like we detect if browser supports local storage and if not then fall back to <body onload="Set_style_from_cookie()"> ) so how do i switch between these two Onload calls ? (I am sorry...not the strongest with js) Regards
With a cookie fallback should localStorage be unavailable or blocked, it would go something like this: var styleHandle = "demo_theme"; function getCookie( name ) { var prefix = name + '='; var begin = document.cookie.indexOf('; ' + prefix); if (begin == -1) { begin = document.cookie.indexOf( prefix ); if (begin != 0) return null; } else begin += 2; var end = document.cookie.indexOf( ';' , begin ); if (end == -1) end = document.cookie.length; return unescape( document.cookie.substring( begin + prefix.length , end ) ); } function retrieveValue (name) { if (localStorage) { return localStorage[name]; } else { return getCookie(name); } } function storeValue( name, value ) { if (localStorage) { localStorage[name]=value; } else { // no need to waste a function on something so simple as setting it document.cookie = 'name' + '=' + escape(value) + ' expires=2147483647'; } } function switchStyle ( styleTitle ) { var linkList = document.getElementsByTagName("link"); for ( var i=0; i<linkList.length; i++ ) { if ( (linkList[i].rel.indexOf('stylesheet') != -1) && (linkList[i].title) ) { linkList[i].disabled = !(linkList[i].title == styleTitle); } storeValue( styleHandle , styleTitle ); } } function onloadHandler() { var styleName = retrieveValue ( styleHandle ); if (styleName) switchStyle( styleName ); } Code (markup): Couple simple function wrappers to test for it. Oh, and if you're curious on the cookie expiration time, that works out to 0x7FFFFFFF in hexidecimal, the highest unsigned 32 bit value -- aka the highest safe value for a cookie expiration on most system -- which works out to sometime in 2039. Thankfully most modern browsers have converted to 64 bit times -- but you never know... 27 years is SO far off... I bet that's what coders were saying in 1973 about Y2K... here it comes, Y2K39
works like a charm....just had to modify Switch_Style function a little bit.... Thanks a million for all your help ! Much appreciated...