{"id":4418,"date":"2016-03-01T18:34:39","date_gmt":"2016-03-01T18:34:39","guid":{"rendered":"http:\/\/www.hostingnotes.com\/?p=4418"},"modified":"2017-10-09T14:03:32","modified_gmt":"2017-10-09T14:03:32","slug":"javascript-functions-creating-and-using-1","status":"publish","type":"post","link":"https:\/\/www.webdevelopersnotes.com\/javascript-functions-creating-and-using-1","title":{"rendered":"JavaScript Functions &#8211; Creating and Using &#8211; 1"},"content":{"rendered":"<p>Functions pack JavaScript statements in a block that can be used over and over again. Any programming language worth its salt allows coders to define and call functions.<\/p>\n<h2>What are javaScript functions? &#8211; An example<\/h2>\n<pre class=\"small-text\">\r\n&lt;SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"TEXT\/JAVASCRIPT\"&gt;\r\n&lt;!--\r\nfunction alert_box()\r\n   {\r\n   alert(\"I am displayed by a function\"); \r\n   document.bgColor=\"#EEEEEE\";\r\n   }\r\n\/\/--&gt;\r\n&lt;\/SCRIPT&gt;\r\n<\/pre>\n<p>A function consists of the <strong><em>function<\/em><\/strong> keyword, the name of the function followed by a pair of parenthesis and lastly, the JavaScript statement\/s enclosed by curly braces.<\/p>\n<p>You can give any name to the function as long as it is not a JavaScript reserved keyword. (The list of reserved keywords can be found <a href=\"\/\/www.webdevelopersnotes.com\/reserved-words-in-javascript\" target=\"_blank\">here<\/a>.)<br \/>The function name can contain only alphanumeric characters (alphabet and digits) and the underscore. Also, the name cannot begin with a numeral.<\/p>\n<p><strong>Some valid function names<\/strong><\/p>\n<ul class=\"withbullets\">\n<li>display_alert_box<\/li>\n<li>calculate<\/li>\n<li>_make_love_not_war<\/li>\n<li>average1<\/li>\n<\/ul>\n<p><strong>Some invalid function names<\/strong><\/p>\n<ul class=\"withbullets\">\n<li>@whats_up_doc &#8211; (uses an invalid character)<\/li>\n<li>123calc &#8211; (function name cannot begin with a numeral)<\/li>\n<\/ul>\n<p>Remember, function names are case sensitive, thus, alert_box is not the same as Alert_box.<br \/>\nThe function block can contain several JavaScript statements enclosed between the curly braces.<br \/>\nThe function in itself does not do anything till we <em><strong>call<\/strong><\/em> it.<\/p>\n<h2>Calling functions<\/h2>\n<p>Calling a function is simple. You have to specify its name followed by the pair of parenthesis.<\/p>\n<pre class=\"small-text\">\r\n&lt;SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"TEXT\/JAVASCRIPT\"&gt;\r\n&lt;!--\r\n\r\n<strong>alert_box();<\/strong>\r\n\r\n\/\/--&gt;\r\n&lt;\/SCRIPT&gt;\r\n<\/pre>\n<p>It&#8217;s good programming practice to place all functions in the HEAD section of the HTML document between the &lt;SCRIPT&gt; &#8211; &lt;\/SCRIPT&gt; tags. Function calls, on the other hand, can occur in any part of the document (where ever they are needed!), even inside event handler code.<\/p>\n<h2>Using a function call inside event handler code<\/h2>\n<p>The code below, calls the function we defined at the beginning of this session. This time we call it thru the <em>onclick()<\/em> event handler.<\/p>\n<pre class=\"small-text\">\r\n&lt;A HREF=\"javascript:void(0)\" <strong>onclick=\"alert_box()\"<\/strong>&gt;\r\nFunction called thru an event handler&lt;\/A&gt;\r\n<\/pre>\n<p>You&#8217;ll notice that a function call looks very similar to calling a method. Now, wasn&#8217;t that simple?<\/p>\n<h2>Opening a new window through a function<\/h2>\n<p>In the previous session, our event handler code for opening a new window had become very long. So instead of writing the code in the HTML tag, we shall place it inside a function and call this function from the event handler.<\/p>\n<pre class=\"small-text\">\r\n&lt;SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"TEXT\/JAVASCRIPT\"&gt;\r\n&lt;!--\r\n\r\nfunction open_win()\r\n   {\r\n   window.open('welcome.html','welcome','width=300,height=200,\r\n   menubar=yes,status=yes,location=yes,\r\n   toolbar=yes,scrollbars=yes');\r\n   }\r\n\r\n\/\/--&gt;\r\n&lt;\/SCRIPT&gt;\r\n<\/pre>\n<p><strong>Note:<\/strong> the entire JavaScript code should be placed on a single line. For clarity, I have put the code on multiple lines.<br \/>\nWe name this function <strong>open_win<\/strong> and place it in the HTML head section.<\/p>\n<pre class=\"small-text\">\r\n&lt;A HREF=\"javascript:void(0)\" onclick=\"open_win()\"&gt;\r\nGet a welcome message&lt;\/A&gt;\r\n<\/pre>\n<h2>Really appreciating functions<\/h2>\n<p>What if you had ten links on a page each opening a new 400X200 pixel window with a different url? Writing separate code for each link can become quite a pain. A simple solution would be to create a function that opens the new window and call it through an event handler. However, we are still left with a problem! How do we instruct the same function to load a different url in each new window? Before we delve deeper into functions let us have a look at variables.<\/p>\n<div class=\"pagenav\">\n<ul>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/creating-or-opening-new-windows-pop-up-in-javascript\" title=\"Creating and opening new pop-up windows with javascript\">&lt; Back<\/a><\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-tutorial\" title=\"JavaScript tutorial\">1<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-event-handlers-part-1\" title=\"Javascript onmouseover and javascript onmouseout event handlers\">10<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-functions-creating-and-using-1\" title=\"Creating and using javascript functions\" class=\"thispage\">13<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-if-statement\" title=\"Javascript IF statement\">20<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/changing-images-on-mouseover-using-javascript\" title=\"Changing images on mouseover using Javascript\">30<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/learning-javascript-programming-variables\" title=\"Javascript Variables - Learning javascript programming\">Next &gt;<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Functions pack JavaScript statements in a block that can be used over and over again. Any programming language worth its salt allows coders to define and call functions. What are javaScript functions? &#8211; An example &lt;SCRIPT LANGUAGE=&#8221;JavaScript&#8221; TYPE=&#8221;TEXT\/JAVASCRIPT&#8221;&gt; &lt;!&#8211; function alert_box() { alert(&#8220;I am displayed by a function&#8221;); document.bgColor=&#8221;#EEEEEE&#8221;; } \/\/&#8211;&gt; &lt;\/SCRIPT&gt; A function consists [&hellip;]","protected":false},"author":1,"featured_media":15641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/comments?post=4418"}],"version-history":[{"count":0,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4418\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media\/15641"}],"wp:attachment":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media?parent=4418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/categories?post=4418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/tags?post=4418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}