Javascript
Javascript
Web Technology-I
Why use client-side
2
programming?
◻ client-side scripting (JavaScript) benefits:
🞑 usability: can modify a page without
having to post back to the server (faster UI)
🞑 efficiency: can make small, quick changes
to page without waiting for server
🞑 event-driven: can respond to user actions
like clicks and key presses
Web Technology-I
What is Javascript?
3
◻ Internal Javascript
<script language=“javascript”
function myFunction()
>
🞑 On load {
script-statements;
🞑 Event-Driven }
</script>
◻ External Javascript
Web Technology-I
JavaScript in
<body>...</body> section
6
</script>
</body>
</html>
Output:-
Hello World
Web Technology-I
Variable
7
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Hello" />
</body>
Web Technology-I
To fetch html form value in
JavaScript
10
<script>
function myFunction()
{
var fnm=document.getElementById(“fname”).value;}
<script>
<form action="">
First name: <input type="text" id="fname" name="fname"
value=""><br>
<input type="button" onclick="sayHello()"
name="button" value="Submit">
</form>
Web Technology-I
JavaScript - RegExp test()
12
Method
◻ The test method searches string for text that
matches regexp.
◻ If it finds a match, it returns true; otherwise, it
returns false.
◻ Syntax: RegExpObject.test( string );
e.g. var regchar=/^[a-zA-Z]*$/;
if(regchar.test(fnm))
var
{
fnm=document.getElementById("fname").value
alert(“You have Entered Alphabet");
} ;
else
{
alert(“You have not Entered Alphabet");
}
Web Technology-I
Return value
13
◻ E.g:
document.getElementById("label1").innerHTML="Please Enter
First Name";
Web Technology-I
15
<button type="button"
onclick='document.getElementById("demo").innerHTML = "Hello
JavaScript!"'>Click Me!</button>
</body>
</html>
Web Technology-I
focus() Method
16
document.getElementById("mobnum").focus();
Web Technology-I
Browser Date()
17
document.getElementById("demo").innerHTML = d;
Web Technology-I
Linking to a JavaScript file:
18
script
<script src="filename.js" type="text/javascript"></script>
Web Technology-I