{"id":30279,"date":"2017-05-21T03:01:05","date_gmt":"2017-05-20T17:01:05","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=30279"},"modified":"2017-05-21T12:25:19","modified_gmt":"2017-05-21T02:25:19","slug":"css-style-javascript-dom-override-onload-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/css-style-javascript-dom-override-onload-primer-tutorial\/","title":{"rendered":"CSS Style Javascript DOM Override Onload Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/are_you_happy_with_my_or_do_you_think_my_derriere_needs_work.html\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"CSS Style Javascript DOM Override Onload Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/are_you_happy_with_my_or_do_you_think_my_derriere_needs_work.jpg\" title=\"CSS Style Javascript DOM Override Onload Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">CSS Style Javascript DOM Override Onload Primer Tutorial<\/p><\/div>\n<p>We&#8217;ve long been fascinated by CSS stylesheets &#8220;living&#8221; with HTML elements in their &#8220;loose&#8221; (but you must admit) very flexible arrangement.  Today we&#8217;re travelling more down that road of discovery of Javascript DOM to see what can be done not using jQuery, but plain old Javascript (clientside) code to change CSS stylesheet settings &#8220;on the <i>onload event<\/i> fly&#8221;, shall we say.  We&#8217;ve explored this topic before when we presented <a target=_blank title='Dynamic Javascript and CSS Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/dynamic-javascript-and-css-primer-tutorial\/'>Dynamic Javascript and CSS Primer Tutorial<\/a>, but today&#8217;s ideas, for which we would like to thank <a target=_blank title='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration'>this great link<\/a> for steering us into the right direction to base the Javascript DOM coding around the <i>CSSStyleDeclaration<\/i> object.<\/p>\n<p>You can tell from the one (critical to the success of methodology) line of code below &#8230;<\/p>\n<p><code><br \/>\nstyleObj = document.styleSheets[k].cssRules[j].style;<br \/>\n<\/code><\/p>\n<p> &#8230; how &#8220;loose&#8221; and &#8220;flexible&#8221; the two &#8230;<\/p>\n<ol>\n<li>HTML element &#8230; and &#8230;<\/li>\n<li>CSS styling<\/li>\n<\/ol>\n<p> &#8230; hang together.  As you might surmise from &#8220;styleSheets[k]&#8221; above you can have many stylesheets involved in a webpage&#8217;s styling (and remember the &#8220;C&#8221; in &#8220;CSS&#8221; stands for &#8220;Cascading&#8221;), and the &#8220;cssRules[j]&#8221; tells you that any one stylesheet &#8220;set&#8221; can contain many CSS Rules, which I guess you can most easily get your head around as a CSS Rule ends with a &#8220;<font color=green>}<\/font>&#8221; before which <font color=red>CSS Styling elements<\/font> are bounded at their start by a &#8220;<font color=orange>{<\/font>&#8221; and before this is the <font color=blue>Parent CSS Rule<\/font> as with &#8230;<\/p>\n<p><code><br \/>\n<font color=blue>body<\/font> <font color=orange>{<\/font><br \/>\n  <font color=red>background-color: orange;<br \/>\n  height: 345px;<\/font><br \/>\n<font color=green>}<\/font><br \/>\n<\/code><\/p>\n<p>What is the mechanism by which we make this a dynamically changeable CSS Styling scenario during the webpage&#8217;s document.body <i>onload<\/i> event triggered logic?<\/p>\n<ol>\n<li>a Javascript function is called at the webpage&#8217;s <i>onload<\/i> event <b>via<\/b> &#8230;<br \/>\n<code><br \/>\n&lt;body <b>onload='forcsscheck();'<\/b>&gt;<br \/>\n<\/code>\n<\/li>\n<li>that code traverses all relevant stylesheets and all relevant rules of those stylesheets and backwards through that rule&#8217;s styling elements to determine styling element <i>name\/value<\/i> pairings, allowing &#8230;<\/li>\n<li>a Javascript prompt window &#8230;<br \/>\n<code><br \/>\nfunction forcsscheck() {<br \/>\n\/\/ Thanks to https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration<br \/>\nif (document.URL.indexOf('isokayasis=') == -1) {<br \/>\nvar styleObj = forcsscheck, j=0, k=0, tval='', cval='', nameString='', i, prule='',title='';<br \/>\nwhile (styleObj != null) {<br \/>\n<b><\/b><br \/>\nwhile (styleObj != null) {<br \/>\nstyleObj = document.styleSheets[k].cssRules[j].style;<br \/>\n<b><\/b><br \/>\nif (styleObj != null) {<br \/>\nprule = styleObj.parentRule.cssText;<br \/>\ntitle = styleObj.cssText;<br \/>\n<b><\/b><br \/>\nfor (i = styleObj.length; i--;) {<br \/>\n  nameString = styleObj[i];<br \/>\n  \/\/styleObj.removeProperty(nameString);<br \/>\n  tval = ('' + title).split(('' + nameString) + ':')[1].split(';')[0];<br \/>\n  cval=prompt('Regarding current CSS Stylesheet definition ... ' + String.fromCharCode(10) + String.fromCharCode(10) + title.split(';')[i] + ';' + String.fromCharCode(10) + String.fromCharCode(10) + ' ... feel free to change the value suggested within the CSS Rule ... ' + String.fromCharCode(10) + String.fromCharCode(10) + prule + String.fromCharCode(10) + String.fromCharCode(10), tval);<br \/>\n  if (cval != null) {<br \/>\n    if (cval != tval) {<br \/>\n       styleObj.setProperty(nameString, cval);<br \/>\n       \/\/alert('Within ' + prule + ' ... ' + nameString + ' changed to ' + cval + ' (from ' + tval + ')');<br \/>\n       document.getElementById('findings').innerHTML+='Within &lt;b&gt;' + prule + '&lt;\/b&gt; ... ' + nameString + ' changed to &lt;b&gt;' + cval + '&lt;\/b&gt; (from ' + tval + ')&lt;br&gt;';<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\nj++;<br \/>\n}<br \/>\n}<br \/>\nstyleObj = forcsscheck;<br \/>\nk++;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\n&#8230; shows what the program knows and allows the user to change a default value that is the current value of the &#8220;name\/value&#8221; pairing as described above, and if the entered value is different &#8230;<\/li>\n<li>CSSStyleDeclaration&#8217;s <i>setProperty(name,value)<\/i> method is called to update the (now dynamic) stylesheet (styling) data associated with the webpage&#8217;s HTML element contents &#8230; and so, voila &#8230;<\/li>\n<li>at the end of interactivity with the user the webpage will show with these new settings as changed, and we add a report of changes down the bottom of our &#8220;proof of concept&#8221; &#8220;first draft&#8221; <a target=_blank title=\"Click picture\" href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/are_you_happy_with_my_or_do_you_think_my_derriere_needs_work.html\">live run<\/a>.  You can download the HTML and CSS and Javascript (DOM) code for <a target=_blank title=\"Click picture\" href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/are_you_happy_with_my_or_do_you_think_my_derriere_needs_work.html_GETME\">are_you_happy_with_my_or_do_you_think_my_derriere_needs_work.html<\/a> to peruse, as well.  We hope it opens some doors, or Windows (chortle I.T.).<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d30279' onclick='var dv=document.getElementById(\"d30279\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/css\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d30279' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve long been fascinated by CSS stylesheets &#8220;living&#8221; with HTML elements in their &#8220;loose&#8221; (but you must admit) very flexible arrangement. Today we&#8217;re travelling more down that road of discovery of Javascript DOM to see what can be done not &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/css-style-javascript-dom-override-onload-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,37],"tags":[155,281,2209,1942,354,576,652,870,997,1761,2210,1211,1319],"class_list":["post-30279","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-body","tag-css","tag-cssstyledeclaration","tag-document-body","tag-dom","tag-html","tag-javascript","tag-onload","tag-programming","tag-prompt","tag-rule","tag-stylesheet","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/30279"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=30279"}],"version-history":[{"count":8,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/30279\/revisions"}],"predecessor-version":[{"id":30400,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/30279\/revisions\/30400"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=30279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=30279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=30279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}