{"id":17137,"date":"2015-09-13T05:01:05","date_gmt":"2015-09-12T19:01:05","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=17137"},"modified":"2015-09-12T14:59:38","modified_gmt":"2015-09-12T04:59:38","slug":"javascript-writes-javascript-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-writes-javascript-primer-tutorial\/","title":{"rendered":"Javascript Writes Javascript Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/jswjs_test.html\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Javascript Writes Javascript Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/jswjs.jpg\" title=\"Javascript Writes Javascript Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Javascript Writes Javascript Primer Tutorial<\/p><\/div>\n<p>Some days back we discovered a use for PHP writing out PHP, though we readily admit there would have been other ways to achieve the same, or a similar solution, when we presented <a target=_blank title='PHP Writes PHP Vertical TextBoxes Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/php-writes-php-vertical-textboxes-primer-tutorial\/'>PHP Writes PHP Vertical TextBoxes Primer Tutorial<\/a>.  This set us to thinking a bit about <i>Javascript writing Javascript<\/i>, and yes, we think there may be uses for this.  However, again, we see that the problem presented today, can be solved in a number of other ways, but just let&#8217;s let the discussion &#8220;stew&#8221; anyway.<\/p>\n<p>On this <i>Javascript writing Javascript<\/i> topic, we found, in practice, three approaches to trying it out (to make something happen (which was to append numbers, of some sort) onto <i>document.title<\/i>) &#8230;<\/p>\n<ol>\n<li>give an HTML script tag in document.head an id=&#8217;myscr&#8217; and later come along and try, a bit before the onload event, to (effectively) &#8230;<br \/>\n<code>document.getElementById('myscr').innerHTML+=' function se' + 'ta() { document.title+=cnt; }  ';<\/code><\/li>\n<li>give an HTML script tag in document.head an id=&#8217;myscr&#8217; and later come along and try, a bit after the onload event, to (effectively) &#8230;<br \/>\n<code>setTimeout(dothing,200); \/\/ where <i>dothing()<\/i> contains same code as for option 1<\/code><\/li>\n<li>use a<br \/>\n<code>document.write('&lt;script&gt; function se' + 'ta() { document.title+=cnt; }  &lt;\/script&lt;');<\/code><br \/>\n statement be given to the Javascript <a target=_blank title='Javascript eval() method information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/jsref_eval.asp'>eval()<\/a> method before the onload event, working off what is supplied via <i>document.URL<\/i> from the web browser address bar<\/li>\n<\/ol>\n<p>So, what do you think happened?<\/p>\n<p>You could try a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/jswjs_test.html\" title='Click picture'>live run<\/a> and see?!  Or, if you have to run (chortle, chortle), and want a quick answer &#8230; click <a id='myahere' href='#myahere' onclick=\" document.getElementById('whathappened').style.display='block'; \" onmouseover=\" document.getElementById('whathappened').style.display='block'; \">here<\/a>.<\/p>\n<div id='whathappened' style='display:none;'>\n<ol>\n<li>Too Hot &#8230; mission failure &#8230; ie. the &#8216;myscr&#8217; HTML script tag was not recognised before the onload event<\/li>\n<li>Too Cold &#8230; mission failure &#8230; ie. the &#8216;myscr&#8217; HTML script tag was already set in its ways after the onload event, that the cache takes over from any intervention you try to make (but maybe you know how to get around this??!!)<\/li>\n<li>Just Right &#8230; mission success &#8230; ie. the new HTML script tag is appended into document.head and is ready to be recognised when the <b>bold<\/b> bit of code as below is accessed &#8230;<br \/>\n<code><br \/>\nfunction sett() {<br \/>\n  document.title+='x';<br \/>\n  <b>if (typeof seta !== 'undefined') {<br \/>\n   seta();<br \/>\n  }<\/b><br \/>\n  setTimeout(sett, 3000);<br \/>\n} <\/code>\n<\/li>\n<\/ol>\n<\/div>\n<p>We leave you with the HTML and (internal) Javascript source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/jswjs_test.html_GETME\" title=\"jswjs_test.html\">jswjs_test.html<\/a> to peruse, for further ideas, we hope.<\/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='#d17137' onclick='var dv=document.getElementById(\"d17137\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=javascript\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d17137' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some days back we discovered a use for PHP writing out PHP, though we readily admit there would have been other ways to achieve the same, or a similar solution, when we presented PHP Writes PHP Vertical TextBoxes Primer Tutorial. &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-writes-javascript-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":[177,1606,352,354,1683,399,438,576,652,870,997,1125,1126,1319],"class_list":["post-17137","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-cache","tag-decodeuricomponent","tag-document-write","tag-dom","tag-dynamic","tag-eval","tag-firebug","tag-html","tag-javascript","tag-onload","tag-programming","tag-setinterval","tag-settimeout","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17137"}],"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=17137"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17137\/revisions"}],"predecessor-version":[{"id":17146,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17137\/revisions\/17146"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=17137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=17137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=17137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}