Web Tech Unit3
Web Tech Unit3
The external resource can be a web page, a picture, a media player, or a plug-in application.
To embed video or audio, it is better to use the <video> and <audio> tags.
Plug-ins
Plug-ins are computer programs that extend the standard functionality of the browser.
data URL Specifies the URL of the resource to be used by the object
type media_type Specifies the media type of data specified in the data attribute
HTML Event
Global Event Attributes
HTML has the ability to let events trigger actions in a browser, like starting a JavaScript when a
user clicks on an element.
To learn more about programming events, please visit our JavaScript tutorial.
Below are the global event attributes that can be added to HTML elements to define event
actions.
Events triggered for the window object (applies to the <body> tag):
onhashchange script Script to be run when there has been changes to the anchor part of the a
URL
onoffline script Script to be run when the browser starts to work offline
ononline script Script to be run when the browser starts to work online
onpagehide script Script to be run when a user navigates away from a page
Form Events
Events triggered by actions inside a HTML form (applies to almost all HTML elements, but is
most used in form elements):
Attribute Value Description
onblur script Fires the moment that the element loses focus
onchange script Fires the moment when the value of the element is changed
onfocus script Fires the moment when the element gets focus
onsearch script Fires when the user writes something in a search field (for
<input="search">)
onselect script Fires after some text has been selected in an element
Mouse Events
onmousemove script Fires when the mouse pointer is moving while it is over an element
onmouseout script Fires when the mouse pointer moves out of an element
onmouseover script Fires when the mouse pointer moves over an element
onwheel script Fires when the mouse wheel rolls up or down over an element
Drag Events
ondragenter script Script to be run when an element has been dragged to a valid drop target
ondragleave script Script to be run when an element leaves a valid drop target
ondragover script Script to be run when an element is being dragged over a valid drop target
ondragstart script Script to be run at the start of a drag operation
Clipboard Events
oncopy script Fires when the user copies the content of an element
oncut script Fires when the user cuts the content of an element
onpaste script Fires when the user pastes some content in an element
Media Events
Events triggered by medias like videos, images and audio (applies to all HTML elements, but is
most common in media elements, like <audio>, <embed>, <img>, <object>, and <video>).
oncanplaythrough script Script to be run when a file can be played all the way to the end
without pausing for buffering
onerror script Script to be run when an error occurs when the file is being
loaded
onloadedmetadata script Script to be run when meta data (like dimensions and duration)
are loaded
onloadstart script Script to be run just as the file begins to load before anything is
actually loaded
onpause script Script to be run when the media is paused either by the user or
programmatically
onplay script Script to be run when the media is ready to start playing
onplaying script Script to be run when the media actually has started playing
onprogress script Script to be run when the browser is in the process of getting the
media data
onratechange script Script to be run each time the playback rate changes (like when a
user switches to a slow motion or fast forward mode)
onseeked script Script to be run when the seeking attribute is set to false
indicating that seeking has ended
onseeking script Script to be run when the seeking attribute is set to true
indicating that seeking is active
Misc Events
Attribute Value Description
ontoggle script Fires when the user opens or closes the <details> element
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
</script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>
</html>
Output
Hello World!
Window Object
If a document contain frames (<iframe> tags), the browser creates one window object for the
HTML document, and one additional window object for each frame.
Window Object Properties
Property Description
defaultStatus Deprecated.
innerHeight Returns the height of the window's content area (viewport) including scrollbars
innerWidth Returns the width of a window's content area (viewport) including scrollbars
localStorage Allows to save key/value pairs in a web browser. Stores the data with no
expiration date
pageXOffset Returns the pixels the current document has been scrolled (horizontally) from
the upper left corner of the window
pageYOffset Returns the pixels the current document has been scrolled (vertically) from the
upper left corner of the window
screenLeft Returns the horizontal coordinate of the window relative to the screen
screenTop Returns the vertical coordinate of the window relative to the screen
screenX Returns the horizontal coordinate of the window relative to the screen
screenY Returns the vertical coordinate of the window relative to the screen
sessionStorage Allows to save key/value pairs in a web browser. Stores the data for one
session
Method Description
confirm() Displays a dialog box with a message and an OK and a Cancel button
getSelection() Returns a Selection object representing the range of text selected by the
user
prompt() Displays a dialog box that prompts the visitor for input
scroll() Deprecated. This method has been replaced by the scrollTo() method.
scrollBy() Scrolls the document by the specified number of pixels
Example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<iframe style="width:100%;height:100px"></iframe>
<iframe style="width:100%;height:100px"></iframe>
<iframe style="width:100%;height:100px"></iframe>
<script>
let length = window.length;
document.getElementById("demo").innerHTML = length;
</script>
</body>
</html>
Output
Property / Method Description
anchors Deprecated
applets Deprecated
charset Deprecated
HTML DOM
When a web page is loaded, the browser creates a Document Object Model of the page.
When you want to access HTML elements with JavaScript, you have to find the elements first.
The easiest way to find an HTML element in the DOM, is by using the element id.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const element = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is: " + element.innerHTML;
</script>
</body>
</html>
Output
The text from the intro paragraph is: Finding HTML Elements by Id
Example
<!DOCTYPE html>
<html>
<body onclick="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
const element = document.activeElement.tagName;
document.getElementById("demo").innerHTML = element;
}
</script>
</body>
</html>
Output
An input field
A Button
BUTTON
Property Description
Method Description
Example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let cookies = navigator.cookieEnabled;
document.getElementById("demo").innerHTML = "Cookies enabled: " + cookies;
</script>
</body>
</html>
Output
The default object of browser is window means you can call all the functions of window by
specifying window or directly. For example:
1. window.alert("hello javatpoint");
You can use a lot of properties (other objects) defined underneath the window object like
document, history, screen, navigator, location, innerHeight, innerWidth,
Form Object
Collection Description
Example
<!DOCTYPE html>
<html>
<body>
<p>Enter some text in the fields below, then press the "Submit form" button to submit the
form.</p>
<form id="myForm" action="/action_page.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="myFunction()" value="Submit form">
</form>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
</script>
</body>
</html>
Output
Screen Object
Property Description
availHeight Returns the height of the screen (excluding the Windows Taskbar)
availWidth Returns the width of the screen (excluding the Windows Taskbar)
colorDepth Returns the bit depth of the color palette for displaying images
pixelDepth Returns the color resolution (in bits per pixel) of the screen
width Returns the total width of the screen
Example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let width = screen.width;
document.getElementById("demo").innerHTML = width + "px";
</script>
</body>
</html>
Output
Build in Object
JavaScript provides various built-in objects. Each object contains properties and methods. Some
of the built-in objects in Javascript are:
Array
Date
Math
String
Number
Array object
Property Description
Method Description
The Math object provides a number of properties and methods to work with Number values.
Among the methods there are sin and cos for trigonometric functions, floor and round for
truncating and rounding the given numbers and max for returning the maximum of given
numbers.
Property Description
PI The value of Pi
Method Description
ceil(a) Rounds up. Returns the smallest integer greater than or equal to a
floor(a) Rounds down. Returns the largest integer smaller than or equal to
a
Date object
At times you there will be a need to access the current date and time and also past and future
date and times. JavaScript provides support for working with dates and time through
the Date object.
Date objected is created using the new operator and one of the Date’s constructors. Current date
and time can be retrieved as shown below:
var today = new Date( );
Methods available on Date object are:
Method Description
Date() Creates a Date object with the current date and time of the
browser’s PC
Date(“Month,dd, yyyy This creates a Date object with the specified string
hh:mm:ss”)
Date(“Month dd, yyyy”) This creates a Date object with the specified string
Date(yy,mm,dd,hh,mm,ss) This creates a Date object with the specified string
Date(yy,mm,dd) This creates a Date object with the specified date
String object
A string is a collection of characters. Most of the times in scripts, there is a need to work with
strings. JavaScript provides various properties and methods to work with String objects.
Whenever a String property or method is used on a string value, it will be coerced to a String
object.
One most frequently used property on String objects is length. The length property gives the
number of characters in the given string. Consider the following example:
var str = “Hello World”;
Method Description
anchor() Create a HTML anchor element around the specified string
big() Create a HTML big element around the specified string
charAt(index) Returns the character at specified index
charCodeAt(index) Returns the unicode value of the character at specified index
Number Object
The number object contains a collection of useful properties and methods to work with Numbers.
The properties include: MAX_VALUE (represents largest number that is available),
MIN_VALUE (represents smallest number that is available), NaN (represents Not a Number),
POSITIVE_INFINITY (special value to represent infinity), NEGATIVE_INFINITY (special
value to represent negative infinity) and PI (represents value of π.
In JavaScript, there are three ways to create user-defined objects to manage enormous tasks and
data sets within an application. They are:
By object literal
By creating an instance of Object directly (using new keyword)
Using object constructor
Object literal is the simplest and the most popular way to create a user-defined object in
JavaScript. We can create a user-defined object with several properties by using object literal, as
follows:
var person = {
firstName: "John",
lastName: "Herry",
age: 25,
skinColor: "White"
};
In the above code, we have created an object called person, which holds four properties:
firstName, lastName, age, and skinColor. The values contained in each property are “John”,
“Herry”, 25, and “White” respectively.
Example
<html>
<head>
<script>
var person = {
firstName:"John",
lastName:"Herry",
age: 25,
skinColor:"White"
};
</script>
</head>
<body>
</body>
</html>
Output:
Age: 25