{"id":12924,"date":"2015-02-24T05:07:28","date_gmt":"2015-02-23T18:07:28","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=12924"},"modified":"2015-02-24T05:07:28","modified_gmt":"2015-02-23T18:07:28","slug":"external-javascript-call-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/external-javascript-call-primer-tutorial\/","title":{"rendered":"External Javascript Call Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/External_Javascript_Call.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"External Javascript Call Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/External_Javascript_Call.jpg\" title=\"External Javascript Call Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">External Javascript Call Primer Tutorial<\/p><\/div>\n<p>Previous relevant <a target=_blank title='PHP + JavaScript + HTML Primer Tutorial' href='#pjhpt'>PHP + JavaScript + HTML Primer Tutorial<\/a> as shown below shows quite a few methods of interaction among PHP and HTML and Javascript to construct webpages on the net.<\/p>\n<p>See you tomorrow &#8230; oh, sorry, forgot &#8230; well you see, today we wanted to hone in on the External Javascript ideas &#8230; the ones with <a target=_blank title='Script tag information from w3schools' href='http:\/\/www.w3schools.com\/tags\/tag_script.asp'>&#8220;script&#8221;<\/a> tags with defined &#8220;src&#8221; parameters.  You see, and you wouldn&#8217;t be alone, that to write External Javascript probably indicates an orderly or modularised mind &#8230; perhaps a love of <a target=_blank title='Lego Oscars performance' href='http:\/\/www.google.com.au\/url?sa=t&#038;rct=j&#038;q=&#038;esrc=s&#038;source=web&#038;cd=1&#038;cad=rja&#038;uact=8&#038;sqi=2&#038;ved=0CB4QqQIwAA&#038;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DCMTwMmlQOP8&#038;ei=h1jrVKSOK9jq8AXZoIKYAw&#038;usg=AFQjCNHu7iz_Blhzd2IfES0XWwuOqcNyOw&#038;sig2=tmePAe4evvw5bFY7JKK7SA&#038;bvm=bv.86475890,d.dGc'>Lego<\/a> <a target=_blank href='https:\/\/www.youtube.com\/watch?v=uVvqO3XhvtU' title='Lego Ikea'>Ikea<\/a>.   Am not going to venture into an opinion one way or the other here, because it depends, as a programmer, how &#8220;you roll&#8221;.   One thing is for certain though, is you&#8217;ll have trouble distributing any of your home-grown Javascript code as a library, unless you develop External Javascript.<\/p>\n<p>So, thinking about External Javascript, you&#8217;d be limiting your scope of programming power if you only think of it as a static &#8220;blob&#8221; of code.<\/p>\n<p>It can be a dynamic &#8220;thang&#8221; and I&#8217;m not just talking about actions taking place after the &#8220;onload&#8221; event of the calling HTML (or HTML derived from PHP).  We&#8217;ll not go into that today, because we&#8217;d be showing nothing new, but have you thought to use External Javascript with a <a target=_blank title='Query string information frim Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Query_string'>&#8220;query string&#8221;<\/a> on the &#8220;src&#8221; parameter of its <a target=_blank title='Script tag information from w3schools' href='http:\/\/www.w3schools.com\/tags\/tag_script.asp'>&#8220;script&#8221;<\/a> tag?  What&#8217;s the point to that?   It means you can have a dynamic Javascript scenario ahead of and independent to the &#8220;onload&#8221; (webpage) logic.   That ability can add great additional functionality to your external Javascript code (which you may be shaping to include in a library of Javascript code, for distribution to others).<\/p>\n<p>Here&#8217;s the HTML of the left hand part of today&#8217;s tutorial picture &#8230; gleaned via a Firebug Copy HTML command &#8230;<\/p>\n<p><code><br \/>\n&lt;script type=\"text\/javascript\" src=\"myjs.js?x6=\/&amp;x3=+&amp;x3=+&amp;x_4=*&amp;x_9=%25&amp;x4=&amp;New%20York=y&amp;Ganges=y\" async=\"async\" data-name=\"jsid55\" id=\"jsid\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>Here&#8217;s the (same scenario) PHP derived HTML of the right hand part of today&#8217;s tutorial picture &#8230; gleaned via a Page-&gt;View Source &#8230;<\/p>\n<p><code><br \/>\n&lt;script id=\"jsid\" data-name=\"jsid97\" async=\"async\" src=\"myjs.js?x6%3D%2F%26x3%3D%2B%26x3%3D%2B%26x_4%3D%2A%26x_9%3D%2525%26x4%3D%26New%2520York%3Dy%26Ganges%3Dy\" type=\"text\/javascript\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>You may be wondering how the client-side HTML came to pass this involved &#8220;script&#8221; &#8220;src&#8221; tag, in the sense that the &lt;head&gt; has already been written by the time a &lt;body&gt; arrives without that more complex &#8220;script&#8221; &#8220;src&#8221; of the original <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.htm_GETME\" title=\"myjs.htm\">myjs.htm<\/a> &#8230; well, we compare via &#8230;<\/p>\n<p><code><br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\n  if (document.head.innerHTML.indexOf(\".js?\") == -1 && document.head.innerHTML.indexOf(\".js\") != -1 && document.URL.indexOf(\"?\") != -1) {<br \/>\n    var parts=document.URL.split(\"?\");<br \/>\n    var oldbody=document.body.innerHTML;<br \/>\n    var newhead=document.head.innerHTML;<br \/>\n    newhead=newhead.replace(\".js\", \".js?\" + parts[1]);<br \/>\n    newhead=newhead.replace('data-name=\"jsid\"', 'data-name=\"jsid' + eval(1 + parts[1].length) + '\"');<br \/>\n    document.head.innerHTML=newhead;<br \/>\n    document.body.innerHTML=oldbody;<br \/>\n  }<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>&#8230; the webpage&#8217;s URL to its proposed external &#8220;script&#8221; tag&#8217;s &#8220;src&#8221; parameter, and as necessary rewrite &#8220;document.head.<a target=_blank title='innerHTML information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/prop_html_innerhtml.asp'>innerHTML<\/a>&#8221; of the current webpage to keep the two in synchronicity &#8230; of course this is a lot simpler to do, equivalently, in PHP (because it comes to us from the server side).<\/p>\n<p>So the external Javascript programming source code is <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.js_GETME\" title=\"myjs.js\">myjs.js<\/a> making heavy use of the Javascript <a target=_blank title='Javascript eval function from w3schools' href='http:\/\/www.w3schools.com\/jsref\/jsref_eval.ASP'>&#8220;eval&#8221;<\/a> function. <\/p>\n<p>The PHP programming source code (using myjs.js as above) is <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.php_GETME\" title=\"myjs.php\">myjs.php<\/a> and here is a <a target=_blank title='Live Run' href='http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.php?x6=\/&#038;x3=+&#038;x3=+&#038;x_4=*&#038;x_9=%25&#038;x4=&#038;New%20York=y&#038;Ganges=y'>live run<\/a> like for the tutorial picture (right side).<\/p>\n<p>The (equivalently functioning) HTML programming source code (using myjs.js as above) is <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.htm_GETME\" title=\"myjs.htm\">myjs.htm<\/a> and here is a <a target=_blank title='Live Run' href='http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/myjs.htm?x6=\/&#038;x3=+&#038;x3=+&#038;x_4=*&#038;x_9=%25&#038;x4=&#038;New%20York=y&#038;Ganges=y'>live run<\/a> like for the tutorial picture (left side).<\/p>\n<p>A useful concept in the Javascript code today is the idea that a var<font size=1>iable<\/font> might be undefined, like PHP can check for with its <a target=_blank title='PHP isset() method' href='http:\/\/php.net\/manual\/en\/function.isset.php'>&#8220;isset&#8221;<\/a> method, and got great help with <a target=_blank title='Useful link' href='http:\/\/stackoverflow.com\/questions\/3390396\/how-to-check-for-undefined-in-javascript'>this link<\/a> and <a target=_blank title='Typeof information from w3schools' href='http:\/\/www.w3schools.com\/js\/js_typeof.asp'>here<\/a>.<\/p>\n<p>Another useful link for this tutorial&#8217;s ideas came from <a target=_blank title='Another useful link' href='http:\/\/cjihrig.com\/blog\/passing-arguments-to-external-javascript-files\/'>this link<\/a>.<\/p>\n<p>Hopefully you can see the power and possibilities of using this <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/External\/External_Javascript_Call.jpg\" title='Click picture'>approach<\/a>.<\/p>\n<p><b><i>Did you know?<\/i><\/b><\/p>\n<p>At one point in the tutorial today it requires the examination of derived HTML as it exists after some Javascript DOM manipulations, and to facilitate this, features the use of the <a target=_blank href='http:\/\/www.mozilla.org\/en-US\/firefox\/new\/' title='Firefox home page'>Firefox<\/a> web browser and a very useful add-on called <a target=_blank title='Firebug home page' href='https:\/\/getfirebug.com\/'>Firebug<\/a> which you may want more information about <a target=_blank title='Firebug tutorials at this blog' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Firebug'>here<\/a>, and which is commonly used to debug client-side Javascript and HTML.   As for today&#8217;s usage, Firebug is also extremely useful in deconstructing how a web page was created.   The other simple wonderful tool for this is the web browser&#8217;s equivalent menu command like View->Page Source (or sometimes equivalent of right-click Page Source).   Firebug has a sister product called <a target=_blank title='FirePHP home page' href='http:\/\/www.firephp.org\/'>FirePHP<\/a> which helps debug server-side PHP and Ajax <a target=_blank title='FirePHP tutorials at this blog' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Firephp'>work<\/a>.<\/p>\n<hr \/>\n<p id='pjhpt'>Previous relevant <a target=_blank title='PHP + JavaScript + HTML Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=550'>PHP + JavaScript + HTML Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php-javascript-html.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"PHP + JavaScript + HTML Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php-javascript-html.jpg\" title=\"PHP + JavaScript + HTML Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">PHP + JavaScript + HTML Primer Tutorial<\/p><\/div>\n<p>PHP is a wonderful language to learn.   It is usually associated with being a web server-side language (as with this tutorial, where it is being shown on a local <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=532'>MAMP<\/a> web server) but can be a <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=262'>command line tool<\/a> as well.   If you like PHP you may eventually like <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=482'>ASP.Net<\/a> and\/or Python, and vice versa.   It has sophisticated data structures, Object Oriented (the thinking that you can build classes with data and methods which define objects created as you run the program &#8230; eg. you might write a class for book and have data members for things like numPages and publisher, author, creationDate and have methods called things like getCreationDate, setCreationDate, getAuthor, setAuthor allowing the user to use these methods rather than changing the  data members themselves &#8230; heaven forbid that!) code concepts, and really combines well with <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=293'>JavaScript<\/a> (as a client-side language).     <\/p>\n<p><a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/php-javascript-html.php_GETME'>Download source code and rename to php-javascript-html.php<\/a><\/p>\n<p><a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/PHP\/php-javascript-html.jpg'>Tutorial<\/a> &#8230;<\/p>\n<p><strong><em>Did you know &#8230;<\/em><\/strong><br \/>\nJavaScript makes a great easy-access Calculator?<\/p>\n<p>Try typing the lines below into the address bar of your favourite browser:<\/p>\n<p>Javascript: eval(512 \/ 380);<br \/>\nJavascript: eval(512 * 380);<br \/>\nJavascript: eval(512 &#8211; 380);<br \/>\nJavascript: eval(512 + 380);<br \/>\nJavascript: eval(512 % 380);<\/p>\n<p>These days we spend so much time on the Internet it is a much quicker way to get to a calculator!\n<\/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='#d550' onclick='var dv=document.getElementById(\"d550\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=PHP\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d550' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\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='#d12924' onclick='var dv=document.getElementById(\"d12924\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Javascript\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d12924' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Previous relevant PHP + JavaScript + HTML Primer Tutorial as shown below shows quite a few methods of interaction among PHP and HTML and Javascript to construct webpages on the net. See you tomorrow &#8230; oh, sorry, forgot &#8230; well &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/external-javascript-call-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":[327,354,399,409,438,439,576,588,652,691,870,932,997,1319,1345],"class_list":["post-12924","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-did-you-know","tag-dom","tag-eval","tag-external-javascript","tag-firebug","tag-firefox","tag-html","tag-ikea","tag-javascript","tag-lego","tag-onload","tag-php","tag-programming","tag-tutorial","tag-url"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/12924"}],"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=12924"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/12924\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=12924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=12924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=12924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}