{"id":883,"date":"2020-06-23T12:08:05","date_gmt":"2020-06-23T16:08:05","guid":{"rendered":"http:\/\/tech.creed3.com\/?p=883"},"modified":"2020-06-23T12:08:05","modified_gmt":"2020-06-23T16:08:05","slug":"get-attention-with-timed-text-alteration-using-javascript-and-css","status":"publish","type":"post","link":"https:\/\/tech.creed3.com\/?p=883","title":{"rendered":"Get Attention With Timed Text Alteration using Javascript and CSS"},"content":{"rendered":"<p>One of the many sites I work on is a genealogy site for <a href=\"http:\/\/www.kykinfolk.com\/breathitt\/\" rel=\"noopener noreferrer\" target=\"_blank\">Breathitt County Kentucky<\/a>. &nbsp;It&#8217;s a simple informational site using only HTML, javascript and CSS. &nbsp;No server side scripting available. &nbsp;It&#8217;s been a good walk back in time for me as I&#8217;ve become so accustomed (read &#8220;dependent&#8221;) on doing everything server side with PHP. &nbsp;Recently an update &#8220;demanded&#8221; some means of calling attention to it without being too annoying or flashy and also without using an image as there was simply no room for it. &nbsp;It also needed to load fast. &nbsp;Enter javascript and CSS.<!--more--><\/p>\n<p>I admittedly don&#8217;t utilize javascript very often. &nbsp;I really dug my heels into it early on (late 1990&#8217;s) but the frustration of varied browser support made me abandon it almost entirely. &nbsp;I much prefer server side scripting as it&#8217;s more controlled and every browser ends up with the same thing. &nbsp;I have dipped a toe or two into ajax over the past couple of years and do really like using it. &nbsp;But in this application, I just needed a little attention getting thing.<\/p>\n<p>In this case it&#8217;s for a new page \/ feature on the site and I wanted to call attention to the menu link for it. &nbsp;There is no room for a simple &#8220;NEW&#8221; image without changing the already wide layout and making it wider. &nbsp;That would be simple. &nbsp;However many of the users for this site are seniors who may not have newer computers with modern browsers or monitors that can properly display a wide format. &nbsp;What I settled on is a simple and constant toggling between the actual link name to a red, bold text &#8220;NEW&#8221;. &nbsp;I spent a little time searching for such a script and came up empty handed so I needed to write it myself.<\/p>\n<p>The javascript is pretty simple and basic:<\/p>\n<pre style=\"font-size:0.9rem;\"><code>&lt;script&gt;\r\nwindow.onload = function(){\r\n\tvar $newtext = document.getElementById('new').innerHTML;\r\n\tsetInterval(() =&gt; {\r\n\t\tsetTimeout(function(){\r\n\t\t\tdocument.getElementById('new').innerHTML= $newtext;\r\n\t\t}, 1500);\r\n\t\tdocument.getElementById('new').innerHTML='<span class=\"red notice\">. . . N E W . . .<\/span>';\r\n\t}, 10000);\r\n}\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>You need to wrap it in a function that loads after the entire page has finished loading otherwise there&#8217;s no chance of being able to get the actual text from within the span tags that you are wanting to call attention to. &nbsp;Once you have it in memory ($newtext) then we run a constantly iterating timing function using setInterval(), and within it a setTimeout() function. &nbsp;Between the two you are able to change the text on screen using document.getElementById().innerHTML. &nbsp;In this case I&#8217;m not only replacing the text but also adding an additional span tag so I can add some additional CSS to the &#8220;NEW&#8221; attention getting text. &nbsp;Since the span which surrounds the new link text is within the hyperlink tag, even while the text is changed to &#8220;NEW&#8221; the link will still function. &nbsp;All are really basic commands any browser will support except possibly the most ancient versions.<\/p>\n<p>The only remaining thing to do is place your span tags around the text you wish to call attention to. &nbsp;In this case it is a menu link:<\/p>\n<pre style=\"font-size:0.9rem;\"><code>&lt; a href=\"mypage.html\" &gt;&lt; span id=\"new\" &gt;Research Requests&lt; \/span &gt;&lt; \/a &gt;<\/code><\/pre>\n<p>Note: ignore the spaces after the < and before the > in the code above. &nbsp;Apparently WordPress doesn&#8217;t want to deal with this as code where a hyperlink is concerned for some reason and I don&#8217;t have the time to spend forcing the issue.<\/p>\n<p>Here it is in action.<br \/>\n<script>window.onload = function(){var $newtext = document.getElementById('new').innerHTML;setInterval(() => {setTimeout(function(){document.getElementById('new').innerHTML= $newtext;}, 1500);document.getElementById('new').innerHTML='<span style=\"color:red;font-weight:bold;\">. . . N E W . . .<\/span>';}, 10000);}<\/script><\/p>\n<blockquote style=\"padding:30px !important;background-color:white;text-align:center;border: solid 1px black;\"><p><a href=\"\"><span id=\"new\">Research Requests<\/span><\/a><\/p><\/blockquote>\n<p>That&#8217;s all folks! &nbsp;Happy coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the many sites I work on is a genealogy site for Breathitt County Kentucky. &nbsp;It&#8217;s a simple informational site using only HTML, javascript and CSS. &nbsp;No server side scripting available. &nbsp;It&#8217;s been a good walk back in time for me as I&#8217;ve become so accustomed (read &#8220;dependent&#8221;) on doing everything server side with [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,16,30],"tags":[37,72],"class_list":["post-883","post","type-post","status-publish","format-standard","hentry","category-css","category-technology","category-web-development","tag-css","tag-javascript"],"views":1647,"_links":{"self":[{"href":"https:\/\/tech.creed3.com\/index.php?rest_route=\/wp\/v2\/posts\/883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.creed3.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.creed3.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.creed3.com\/index.php?rest_route=\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.creed3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=883"}],"version-history":[{"count":0,"href":"https:\/\/tech.creed3.com\/index.php?rest_route=\/wp\/v2\/posts\/883\/revisions"}],"wp:attachment":[{"href":"https:\/\/tech.creed3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.creed3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.creed3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}