Adding a cookie to an existing Javascript external CSS file swap -
i created function swap css files, works until comes reloading page or going page on website. figured while might able away not adding cookie, might clients close site , return later without having swap 'site theme' of choice.
here's existing function:
function changecss(cssfile, csslinkindex) { var oldlink = document.getelementsbytagname("link").item(csslinkindex); var newlink = document.createelement("link"); newlink.setattribute("rel", "stylesheet"); newlink.setattribute("type", "text/css"); newlink.setattribute("href", cssfile); document.getelementsbytagname("head").item(0).replacechild(newlink, oldlink); }
so, can cookie use existing newlink variable , attributes, or need create variable in 'set cookie' function determine newlink variable?
i wouldn't swap css, instead make "base theme" same users , apply "skin" on top of theme, have more maintainable code , don't have remove/re-add stylesheets. append new stylesheet on demend when user select favorite skin.
anyway use library https://github.com/carhartl/jquery-cookie if use jquery , set or change value of cookie on fly when user change skin. script check cookie during page load , append preferred skin accordingly.
ps if want avoid fouc (flash of unstyled content) need introduce somehow loading or whole thing server-side.
Comments
Post a Comment