Using JavaScript Function in PHP

Introduction

 
How to use JavaScript function in HTML. JavaScript is the world's most popular programming language. It is the language for HTML and PHP. A function is a block of code that will be executed. The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.
 
Syntax
 
A function is written as a code block inside curly { } braces.
  1. function functionname()  
  2. {  
  3. some code to be executed  
Function calling with arguments
 
When you call a function, you can pass values to it. These values are called arguments or parameters.
 
These arguments can be used inside the function.
 
You can pass many arguments as you like, separated by commas (,).
  1. Functionname(argument1,argument2) 
Declare the arguments, as variables, when you declare the function.
 
Access a JavaScript variable from HTML
  1. <--Javascript file with HTML-->  
  2. <html>  
  3. <head>  
  4. <script type="text/javascript">  
  5.     function myday() {  
  6.         var d = new Date();  
  7.         var weekday = new Array(7);  
  8.         weekday[0] = "Sunday";  
  9.         weekday[1] = "Monday";  
  10.         weekday[2] = "Tuesday";  
  11.         weekday[3] = "Wednesday";  
  12.         weekday[4] = "Thursday";  
  13.         weekday[5] = "Friday";  
  14.         weekday[6] = "Saturday";  
  15.    
  16.         var x = document.getElementById("ram");  
  17.         x.innerHTML = weekday[d.getDay()];  
  18.     }  
  19.     function showDate() {  
  20.         var today = new Date()  
  21.         var d1 = new Date("October 13,1975 11:13:00");  
  22.         var x = document.getElementById("ram"); x.innerHTML = [d1.getDate()];  
  23.     }  
  24.     function myFunction() {  
  25.         var month = new Array();  
  26.         month[0] = "January";  
  27.         month[1] = "February";  
  28.         month[2] = "March";  
  29.         month[3] = "April";  
  30.         month[4] = "May";  
  31.         month[5] = "June";  
  32.         month[6] = "July";  
  33.         month[7] = "August";  
  34.         month[8] = "September";  
  35.         month[9] = "October";  
  36.         month[10] = "November";  
  37.         month[11] = "December";  
  38.         var d = new Date();  
  39.         var x = document.getElementById("ram");  
  40.         x.innerHTML = month[d.getMonth()];  
  41.     }  
  42.     function displayyear() {  
  43.         var d = new Date();  
  44.         var x = document.getElementById("ram");  
  45.         x.innerHTML = d.getFullYear();  
  46.     }  
  47. </script>  
  48. </head>  
  49. <body>  
  50.    
  51. <div align="center">  
  52. <table border="2" height="20%" width="20%" bgcolor="#FDF2C8">  
  53. <tr>  
  54. <th><h1><font color="#DC8347">How to use JavaScript</h1></th>  
  55. <p id="ram"><b>Display on here</b></p>  
  56. <p id="P1"></p>  
  57. <p id="P2"></p>  
  58. <p id="P3">.</p>  
  59. </tr></table>  
  60. <button onClick="myday()">Day</button>  
  61. <button type="button" onClick="showDate()">Date</button>  
  62. <button onClick="myFunction()">Month</button>  
  63. <button onClick="displayyear()">Year</button>  
  64.    
  65. </body>  
  66. </html> 
Javascript function with html1.jpg
 
Output
 
Javascript date function with html1.jpg
 
Javascript day function with html1.jpg
 
Javascript Month function with html1.jpg
 
Javascript Year function with html1.jpg