{"id":649,"date":"2014-06-18T11:07:56","date_gmt":"2014-06-18T11:07:56","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=649"},"modified":"2014-06-18T11:07:56","modified_gmt":"2014-06-18T11:07:56","slug":"stylesheet-easy-swap-method","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=649","title":{"rendered":"stylesheet easy swap method"},"content":{"rendered":"<blockquote><p>Goal: tricky way of stylesheet change on a page<\/p><\/blockquote>\n<p>I&#8217;ve read many blogs about this theme, but I&#8217;m not satisfied.<br \/>\nHere is my way. Short and effective.<br \/>\nMy blog has a cloudy design, but I have an other image in my head with a golf ball. I don&#8217;t want to drop the first one and I want to realize the second one as well. Basically the background image different and some page element&#8217;s color out of the body box.<br \/>\n<strong>1. create the base<\/strong><br \/>\nI need a new stylesheet file with the modified elements (this file named as golf.css):<\/p>\n<pre data-enlighter-language=\"css\" class=\"EnlighterJSRAW\">\r\n#sidebar a {\r\n  color:#fff;\r\n}\r\n#sidebar ul ul li {\r\n  color: #DEF585;\r\n}  \r\n#sidebar h2 {\r\n  color: #fff;\r\n}\r\n#sidebar ul p, #sidebar ul select {\r\n  color: #BEDDBE;\r\n}\r\n#backfly {\r\n  background: url(&quot;images\/golfBallWallPaper.jpg&quot;);\r\n}\r\n#cssgolf{ \/* this one put to the original stylesheet file ! *\/\r\n  display: none;\r\n}\r\n<\/pre>\n<p>and I will include this file into an identified but not valid style block:<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n&lt;stylex id=&quot;cssgolf&quot;&gt;\r\n&lt;?php include_once('css\/golf.css'); ?&gt;\r\n&lt;\/stylex&gt;\r\n<\/pre>\n<p>This is not a valid style block (I need to hide it &#8211; #cssgolf{ display: none; }) &#8211; no effects on the page!<br \/>\n<strong>2. play with the style<\/strong><br \/>\nI put a switcher somewhere to the page (beside the blog name a small icon &#8211; golf ball <img decoding=\"async\" src=\"images\/golf.png\" style=\"vertical-align:middle;\" \/> or clouds <img decoding=\"async\" src=\"images\/cloud.png\" style=\"vertical-align:middle;\" \/>) and I will put the following code behind this control:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n...\r\n    var stx = true,\r\n        ff = \/firefox\/i.test(navigator.userAgent)\r\n          || \/msie\/i.test(navigator.userAgent); \/\/ check if ff or ie\r\n        \r\n    $('#schg').click(function(){\r\n    \r\n      var t = $('#cssgolf')[0].outerHTML;\r\n      if(stx | ff){\r\n        if(stx){\r\n          $('#cssgolf')[0].outerHTML= t.replace(\/stylex\/g,'style');\r\n        }\r\n        else{\r\n          $('#cssgolf')[0].outerHTML = t.replace(\/style\/g,'stylex');\r\n        }\r\n      }\r\n      else{\r\n        document.styleSheets['cssgolf'].disabled = ! document.styleSheets['cssgolf'].disabled;\r\n      }\r\n      stx = !stx;\r\n\r\n      $(this).attr('src', stx ? 'images\/golf.png' : 'images\/cloud.png');\r\n      \r\n      cookies.set('css', stx ? 0 : 1);\r\n\r\n    });\r\n    \r\n    if(cookies.get('css') == 1){$('#schg').click();}\r\n...\r\n<\/pre>\n<p><strong>3. how it&#8217;s working<\/strong><br \/>\nI have an invalid style block without noticable effects. This is good at the start.<br \/>\nWhen I want to fire the new style I need to activate\/fix that. Just replace the invalid <strong>stylex<\/strong> tag with the valid <strong>style<\/strong> word (this is simple with regex) and the new style is working now.<\/p>\n<p>After this point there are two ways to follow:<br \/>\n&#8211; I can disable the style block by the ID (chrome, opera working, <span class=\"red\">but not in firefox and ie<\/span>)<br \/>\n&#8211; I need to invalidate the style block again to disable it (firefox, ie) &#8211; that&#8217;s why I check the browser is ff\/ie at the second line in the code.<br \/>\n&#8230;may be this is the better solution because this works in all browsers&#8230;<\/p>\n<p>Of course I will change the switcher image depends on the selected style. Done.<\/p>\n<p><strong>or<\/strong><\/p>\n<p>Here is a soft version of this solution detailed above.<br \/>\nSoft because the new style block is a valid one but has a special condition which is prevents applying. This style block will be inactive every time. The never matching condition e.g. device-width: 1px &#8211; like this:<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n&lt;style id=&quot;golf&quot; media=&quot;screen and (device-width:1px)&quot;&gt;\r\n&lt;?php include_once('css\/golf.css'); ?&gt;\r\n&lt;\/style&gt;\r\n<\/pre>\n<p>references for available conditions: <a href=\"http:\/\/www.w3schools.com\/tags\/att_style_media.asp\" target=\"_blank\">jump to read<\/a><\/p>\n<p>In this case this is an inactive style block again.<br \/>\nWe can activate this with dropping the bad condition (just clear or drop the media tag).<br \/>\nAnd after this point the method is the same. Disable or inactivate the style block again and again&#8230;<\/p>\n<p>see also: <a href=\"?p=647\" title=\"simple cookie manager class\">cookies class<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: tricky way of stylesheet change on a page I&#8217;ve read many blogs about this theme, but I&#8217;m not satisfied. Here is my way. Short and effective. My blog has a cloudy design, but I have an other image in my head with a golf ball. I don&#8217;t want to drop the first one and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,11],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-css","category-tricky"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=649"}],"version-history":[{"count":1,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/649\/revisions\/650"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}