29
Monday, October 13, [email protected] 1 Basics of HTML (Hyper Text Markup Language) HTML is used to render Web pages Rendering may differ from a browser to another It has no computation power and should include some Scripting language, like Java Script or VBScript to produce User interactive Rendering Pages Despite several design tools, we should learn some basics of HTML to be good web developer.

Html javascript

Embed Size (px)

Citation preview

Monday, October 13, [email protected] 1

Basics of HTML (Hyper Text Markup Language)

HTML is used to render Web pagesRendering may differ from a browser to another It has no computation power and should include

some Scripting language, like Java Script or VBScript to produce User interactive Rendering Pages

Despite several design tools, we should learn some basics of HTML to be good web developer.

Monday, October 13, [email protected] 2

Some basic tags of HTML

The MIME type is text/html (details on MIME later on)

Apart from <html>, <head>, <title>, <body>, and 6 heading tags like <h1>…<h6>, we have tags like <form>, <b>, <u>, <i>, <a>, <ul>, <ol>, <table>, <input>, <select> et al, to mention a few.

Not all these tags require closing, but it’s a good practice to close all tags when over.

Most of the tags have attributes, and it’s a common malpractice to avoid double/single quotes enclosing the attribute values.

Monday, October 13, [email protected] 3

Hey Man! Quotes Must!

For Example, <input type=“text” name=“fname” value=“Aditya Narayan”> is Okay.

But, not the following one… <input type=text name=fname value=Aditya Narayan>

In the second case, for some browsers shall truncate the part Narayan from the field.

Monday, October 13, [email protected] 4

Form Tag and its Importance

The main power of Web is how easily and fast it interacts with the server and processes the user inputs, and what transfers data from the client to the server is the <FORM> tag.

It defines mainly 2 attributes, though not mandatory but expected, are action and method. By default method is GET (details later)

The action contains the URL that the FORM is submitted to and transfers the user-input data to.

Form may contain some <input> field as shown in next slide, some <select> field, and a Submit Button or a Button leading to the Submit event.

Monday, October 13, [email protected] 5

Inside a <FORM> tag<html><head><title>Welcome to JISCE</title></head><body><form action=http://myhost:myport/myURI><pre>Enter name: <input type=“text” name=“myName”>Enter ID: <input type=“text” name=“myId”><select name=“mySkill”><option value=“Java SE”>Java SE 1.4</option><option value=“Java EE”>Java EE 1.4</option><option value=“Java ME”>Java 2 ME </option></select>Gender <input type=“radio” name=“mySex” value=“Male”>Male<input type=“radio” name=“mySex” value=“Female”>FemaleCheck if already registerd <input type=“checkbox”

name=“regStatus” value=“regd”><input type=“Submit” value=“Done”></pre></form></body></html>View At the Code above rendered in IE 6

Monday, October 13, [email protected] 6

Inside the <INPUT> tag

<INPUT> tags don’t need to be closed. With this, we can take any text data, password,

checkbox, radio button, Submit Button, Hidden Form Field, Any Button, A File to Upload etc.

The mandatory “type” attribute determines what the INPUT is going to render.

Example, <input type=“text” name=“fname”> The name attribute is must, too. It lets the server

fetch the data “fname” from the data entered here

More on these follows…

Monday, October 13, [email protected] 7

More on <INPUT> tag

<input type=“password” name=“pwd”><input type=“radio” name=“sex” value=“Male”>

Male<input type=“radio” name=“sex”

value=“Female”>Female The above 2 radios must have same nameelse both can be selected at time that’s not desiredThe value attribute must be present with radios

and You have to render, too; else the user shall not see what’s for what option. The 2 need not be same.

Monday, October 13, [email protected] 8

<INPUT> tag…contd. <input type=“checkbox” name=“skill” value=“Java

SE”>Java SE<input type=“checkbox” name=“skill” value=“Java

EE”>J2EE<input type=“checkbox” name=“skill” value=“Java

ME”>J2ME Check boxes with same name can be multi-checked at

a single time. They must have values and the rendered text should be there to tell the user what’s what option and the value attribute may not be same as the text rendered. The server shall accept the value attribute’s values.

Here, the server shall consider the “Java EE” but not the rendered text J2EE.

Monday, October 13, [email protected] 9

<INPUT> tag… contd.

<input type=“hidden” name=“hdn1” value=“logged”> This shall not be rendered in the browser but are often required for Session tracking (details later)

<input type=“submit” value=“Done”><input type=“button” name=“btn1”

value=“Click”><input type=“file” name=“upldFile”> shall

open a File type input on browsing.

Monday, October 13, [email protected] 10

<select> Tag as Drop-down list

<select name=“skill”><option value=“Java”>Java</option><option value=“CPP”>C++</option><option value=“C#”>C-Sharp</option></select> It should have a name, so to let the server

retrieve the data skill from the form, after it’s submitted.

Also, Server shall get the value “CPP”, not “C-Sharp”

Monday, October 13, [email protected] 11

Java Script for Client Side Validation

Sometimes, you can’t rest assured with the data that the user inputs through the client browsers as they have to pass several validation and conform to certain constraints. Like, the user may be required to fill up all the fields in the form and then submit. Also, the user may input some Alphabets where only numeric data is required. All these things must be taken care of by the server side program.

But, better if we could check them at the client side, thus saving the time of a round-trip to the server. Scripting languages like JavaScript used.

Monday, October 13, [email protected] 12

A Simple Form validation with Java Script

<script language=“JavaScript”>function chkForm(){

var msg=“”; if(document.forms[0].myName.value==“”){

msg+=“Name can’t be blank!\n”;}

if(document.forms[0].myCity.value==“”){msg+=“City can’t be blank!”;

} if(msg==“”) return true; else{

alert(msg);return false;

}}</script>

Monday, October 13, [email protected] 13

The HTML Code

<body><form action=“myURL” onsubmit=“return chkForm()”>Enter Name: <input type=“text” name=“myName”><br>Enter City: <input type=“text” name=“myCity”><input type=“submit” value=“Submit”></form></body></html>FormValidation.html onsubmit=“return chkForm()” Note the return

keyword, without which it’d not work!

Monday, October 13, [email protected] 14

Netscape Navigator 2 (support for java applets and LiveScript renamed as JavaScript1.1)

Netscape developed a scripting language called LiveScriptSupported both client side and server side scripting

Microsoft- JScript IE 3

1997 ECMA (European Computer Manufacturers Association)Standardized ECMA script

W3C standardized DOM to access HTML and XMLJavaScript 1.2

Opera supporting JavaScript 1.1

Brief history of JavaScript

Monday, October 13, [email protected] 15

Response

Client Web Server

Request

HTML file

JAVA SCRIPT

Script

executes

locally and

interacts with

the browser

Programs executes on the server and sends the response

Servlet files

JSP files

HTML files

Monday, October 13, [email protected] 16

Uses• To create more interactive pages- client side validations etc.• To generate html dynamically.• Event handling• To enhance browser capabilities by giving it a better look –

printing on status bar etc.• Interaction with embedded components like applets and active

x controls.Language Features

• Object based language:no object inheritance and subclassing. Object -based because it depends on built-in objects to function.

• Semicolon, as separator for multiple statements in the same line.

• Syntax similar to c++ and java

• Case sensitive

• Loosely typed

• Platform independent

• Interpreted

Monday, October 13, [email protected] 17

External Script<HTML><HEAD><BODY> <SCRIPT LANGUAGE=“JavaScript”

SRC=“JSfile.js”></SCRIPT></BODY></HTML>

JSfile.js

document.write(“Hello”)

Scripts inside body and headInside head only declarations should be done. No write

statements must appear inside head tag.<HTML><HEAD><TITLE>Hello</TITLE><SCRIPT>document.write(“Hello java script”)</SCRIPT></HEAD><BODY></BODY></HTML>

Incorrect

Monday, October 13, [email protected] 18

Java Script Data Type and Scope

Java Script is loosely typed and it supports Data Types like String, Date, Array etc. and totally objects.

A variable defined with keyword var inside a function is a local variable. Otherwise it has scope global to the entire HTML page.

We, however, must not indulge in the luxury of dealing with Language basics.

Monday, October 13, [email protected] 19

A Simple variable and function example

<html><head></head><script>age=90;function fun(){

age=26;alert("Age = "+age);

}</script><input type="button" value="Show Age" onclick="fun()"><input type="button" value="Show Age12" onclick="sd()"><script>function sd(){alert("Age = "+age);}</script><body></body></html> See this execute

Monday, October 13, [email protected] 20

Continuation to The Code

Here age has global scope. Now if we modify the code as including

function fun(){

var age=26;

alert("Age = "+age);

} we shall get different output.

If we omit the statement age=90; we get different situation. But if we associate a var with age, it’d be same.

After this example we not only got to learn about the scope of variables, but we proved that declaring a variable with var keyword doesn’t always localize it.

Monday, October 13, [email protected] 21

Event handling with Java Script

There’s nothing like consulting the documentation of Java Script. We shall learn to use Java Script with HTML with hands-on practice.

Sometimes we shall need to embed Java Script in HTML and position elements dynamically.

Some event-handlers: onclick, onkeyup, onkeypress, onkeydown, ondblclick, onchange,

Following shall be some assignments we’d discuss.

Monday, October 13, [email protected] 22

Assignments on Java Script Event Handling-1

There’ll be a form having 2 text fields and a checkbox. The 2nd text needs only numbers. So, Typing sans numeric shall not be allowed. Now, the checkbox if checked, on clicking the submit button, the browser will ask for confirmation whether to submit or not. If both the fields are not filled up, the form shall never be submitted.

Monday, October 13, [email protected] 23

Assignments on Java Script Event Handling-2,3

There should be a Hyper link that links to a page. As you make the mouse cursor go over the link, it’d change font color and size. As you mouse-out, it should be restored.

There should be a form that takes 2 text fields, name and Phone No. The Phone No. must be numeric. Also, there should be a checkbox asking for if there is an alternative phone Number to be provided, which, when checked, will display another text field taking only numbers and un-checking will make it disappear.

Monday, October 13, [email protected] 24

Assignments on Java Script Event Handling-4,5

There should be a text area, and you can type up to 100 characters. As you keep on typing, a text indicating how many characters more remain, shall be displayed continuously.

There should be 2 text fields taking two numbers. A button should be there disabled until both the text fields are filled up. However, if you delete any of the numbers, the button should become disabled again. Otherwise, clicking the button, the sum should be displayed.

Monday, October 13, [email protected] 25

Frames and Java Script: Assignment

There should be 2 frames on a page. The LHS frame should contain link to a page say 2.html, in various ways, to open in the same and parent frames, in a new window, and in the same window. The page 2.html, would have buttons to close the window in various ways.

You would need some knowledge on DHTML and Cascading Style Sheets (css) to solve some of the assignments. So, the next few slides follow…

Monday, October 13, [email protected] 26

Three ways to include style• Embedded style

• Inline style

• Linked stylesSetting up style info- an example

Microsoft wayBODY { font: 12 pt Arial; color: navy; margin-

left: 0.25in }

H1 { font: 18 pt Arial; color: red }

Monday, October 13, [email protected] 27

Embedded style : <Style> tag<html><head>

<style type="text/css">

BODY { font: 12 pt Arial; color: navy; margin-left: 0.25in}

H1 { font: 18 pt Arial; color: red}

</style>

</head>

<body>

<h1>Dynamic Web Pages</h1>

The need of dynamic web pages; an overview of DHTML,

cascading style sheet, comparative studies of different technologies of

dynamic page creation

</body></html>

Monday, October 13, [email protected] 28

Inline style

<table style="font: 12 pt Arial; color: green; font-weight: bold">

<tr><td>Name</td><td>Reg No. </td></tr>

<tr><td>XXXX</td><td>55555</td></tr>

</table>

Monday, October 13, [email protected] 29

Linked style• Linking to a style info in a separate file.

BODY { font: 12 pt Arial; color: navy; margin-left: 0.25in}

H1 { font: 18 pt Arial; color: red}

style.css

<html><head>

<link rel=“stylesheet” href=“style.css”>

</head>

<body><h1>Dynamic Web Pages</h1>

The need of dynamic web pages; an overview of DHTML,cascading style sheet, comparative studies of different technologies of dynamic page creation

</body></html>