Jump to content

JS cookies w stylesheets??


skaterdav85
 Share

Recommended Posts

So i created 2 links that give the user 2 options of different style sheets, and i save whatever style sheet chosen as a cookie. However, how do i read those style sheets for the rest of my site? this is the code where it saves the style sheet chosen as a cookie://initial style sheet<link rel="stylesheet" href="style_1.css" id="stylesheet">//saves the chosen style sheet as a cookiefunction changeStyle(whichsheet) {document.cookie = document.getElementById('stylesheet').href = whichsheet;}//2 links for different style sheet options<a href="#" onClick="changeStyle('style_2.css');">style sheet 2</a><a href="#" onClick="changeStyle('style_3.css');">style sheet 3</a>Thanks!

Link to comment
Share on other sites

function getCookie(k) {var d = document.cookie || "";var pairs = d.split(k + "=");if (pairs && pairs[1]) {return unescape(pairs[1].split(";")[0]);}};function setCookie(nm, valu) {var e2 = (new Date(2019, 1, 1)).toGMTString();document.cookie = nm + "=" + escape(valu) + "; expires=" + e2;}function changeStyle(whichsheet) {  whichsheet = whichsheet || 'style_1.css'; //set up a first-visit default  setCookie( "lastCSS", whichsheet);}<body onload="changeStyle(getCookie('lastCSS'));">

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...