javascript - How to make my stylesheet switch, stick -
i have written code change stylesheets between normal 1 , accessible version. in theory works, refresh page goes default version. know need store cookie of sort , i've tried couple of scripts i've found on google, none work. if has idea of need in order selected stylesheet stick throughout site until other requested, appreciated.
code far:
<link type="text/css" href="style.css" rel="stylesheet" title="normal" /> <link type="text/css" href="css/accessible.css" rel="alternate stylesheet" title="accessible" /> <form> <input type="submit" onclick="switch_style('normal');return false;" name="theme" value="" id="normal" title="view site in it's original format"> <input type="submit" onclick="switch_style('accessible');return false;" name="theme" value="" id="accessible" title="view site in it's simplest form"> </form>
this javascript i've incorporated, seems have bug in somewhere, can't life of me find it:
var style_cookie_name = "style" ; var style_cookie_duration = 30 ; function switch_style ( css_title ) { var i, link_tag ; (i = 0, link_tag = document.getelementsbytagname("link") ; < link_tag.length ; i++ ) { if ((link_tag[i].rel.indexof( "stylesheet" ) != -1) && link_tag[i].title) { link_tag[i].disabled = true ; if (link_tag[i].title == css_title) { link_tag[i].disabled = false ; } } set_cookie( style_cookie_name, css_title, style_cookie_duration ); } } function set_style_from_cookie() { var css_title = get_cookie( style_cookie_name ); if (css_title.length) { switch_style( css_title ); } } function set_cookie ( cookie_name, cookie_value, lifespan_in_days, valid_domain ) { var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ; document.cookie = cookie_name + "=" + encodeuricomponent( cookie_value ) + "; max-age=" + 60 * 60 * 24 * lifespan_in_days + "; path=/" + domain_string ; } function get_cookie ( cookie_name ) { var cookie_string = document.cookie ; if (cookie_string.length != 0) { var cookie_value = cookie_string.match ( '(^|;)[\s]*' + cookie_name + '=([^;]*)' ); return decodeuricomponent ( cookie_value[2] ) ; } return '' ; }
there small errors in get_cookie function.
remove:
function get_cookie ( cookie_name ) { var cookie_string = document.cookie ; if (cookie_string.length != 0) { var cookie_value = cookie_string.match ( '(^|;)[\s]*' + cookie_name + '=([^;]*)' ); return decodeuricomponent ( cookie_value[2] ) ; } return '' ; }
replace with:
function get_cookie ( cookie_name ) { var cookie_string = document.cookie ; if (cookie_string.length != 0) { var cookie_value = cookie_string.match( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' ); return decodeuricomponent ( cookie_value[2] ) ; } return '' ; }
hope helps or else same problem.
Comments
Post a Comment