Introduction to Java Script
About JavaScript
JavaScript is not Java
The original name for JavaScript was LiveScript Its syntax resembles java syntax The name was changed when Java became popular
Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C language
Using JavaScript in a browser
JavaScript code is included within <script> tags:
<script type="text/javascript"> [Link]("<h1>Hello World!</h1>") ; </script> Or just within <script> and </script>
Notes:
The type attribute is to allow you to use other scripting languages (but JavaScript is the default)
Where to put JavaScript
JavaScript can be put in the <head> or in the <body> of an HTML document
JavaScript functions should be defined in the <head> This ensures that the function is loaded before it is needed JavaScript in the <body> will be executed as the page loads
Objects & Functions Event Handling Form Validation
Event Handling
CSS with Java Script
<div style="backgroundcolor:#D94A38;width:170px;height:80px;margin:20px;paddingtop:20px;color:#ffffff;font-weight:bold;font-size:18px;float:left;textalign:center;" onmouseover="[Link]=Non Major Elective'" onmouseout="[Link]='Mouse Over Me'">Offered by University Informatics Centre</div>
Click Event
<!DOCTYPE html> <html> <head> <script> function myfun(id) { [Link]="<b><u><em>Click to draw an underline and make it Italic!</em></u></b>"; } </script> </head> <body> <h1 onclick="myfun(this)">Click to draw an underline and to make it Italic!</h1> </body> </html>
Form Handling
<!DOCTYPE html> <html> <head> <script> function ff() { var x=[Link]["sample"]["sname"].value; if (x==null || x=="") { alert("Student name must be filled out"); return false; } } </script>
<form name="sample" action="[Link]" onsubmit="return ff()" method="post"> Student name: <input type="text" name="sname"> <input type="submit" value="Submit"> </form>
Q and A