JAVASCRIPT IN HTML NOTES
[Link] in the blanks.
a. JavaScript is an example of a ------------- scripting language.
Ans. client-side
b. PHP and [Link] are examples of ----------scripting language.
Ans. Server-side
c. Which tag is used to include an internal JavaScript code in an HTML file?
Ans.<script></script>
d. What is the correct way to create a variable in JavaScript?
Ans. Var variable=”value”;
e .Which function is used to display a dialog box in JavaScript?
Ans. alert()
[Link] the Following.
a. Two DOM methods:
Ans. [Link](),[Link]()
b. Two JavaScript Events:
Ans. onclick event ,onmouseover event
c. Two uses of JavaScript
Ans. To make webpages interactive,For Game development
d. Two examples of client-side scripting language
[Link],jQuery
[Link] the following questions:
a. What is JavaScript used for? List any two features of JavaScript.
Ans. JavaScript is one of the most popular client-side scripting language. It
is commonly used to build web applications and even games. Two features
of JavaScript are:
JavaScript is a scripting language that is interpreted, meaning it can
run directly without being compiled. The script is executed one line
at a time.
It is a client side scripting language that runs on a client’s computer
in a browser.
b. What is the advantage of using external JavaScript over internal
JavaScript?
Ans. The main advantage of external JavaScript over internal JavaScript is
that external JavaScript allows us to organise our script properly ,reuse the
script you have created and maintain your script more easily.
c. Differentiate between alert() and prompt() function.
Ans. alert() function is used to display a dialog box with a message and
an OK button. It is a simple way to show a message to a user. Whereas the
prompt() function is used to display a dialog box with a message and an
input field where the user can enter data.
d. Karan has created a website containing more than 10 items in a list. He
wants to highlight specific items in a different colour. Which DOM method
should he use in this case? Justify your choice.
Ans. Karan can use the getElementByClassName() method for performing
this task. He can give the same class name for the specific items to get
highlighted in a different colour. Later for that particular class items he
can set the desired colour for highlighting.
e. Write a JavaScript function to calculate the area of a rectangle whose
length and breadth are passed to it as parameters .The length and
breadth should be accepted from the user using prompt().The area of the
rectangle should be displayed in an alert() message.
Ans.
Extra Questions:
1. What is a scripting language?
Ans. A scripting language is a programming language mainly
used to create interactive and dynamic websites and web
applications.
Example: To add pop-up windows and dialog boxes in a
webpage.
2. What are the two types of scripting languages?
Ans. Client-side scripting: A client side scripting language runs on a
client’s computer on a browser .The script will be placed inside an
HTML document. JavaScript and jQuery are the examples of Client side
scripting language.
Server -side scripting: A scripting language that runs on a
web server is called server side scripting language and [Link] are the
examples of Server-side scripting languages.
3. What are the two ways in which JavaScript can be used in an
HTML file?
Ans. Internal JavaScript: Internal JavaScript refers to a script that is
added within an HTML file, either in the <head> or <body> section. It
is called internal because the code is contained inside the HTML file
itself.
Example: page no.92 of textbook.
External JavaScript: External JavaScript refers to a JavaScript code that
is written in a separate file with the extension .js and then linked to an
HTML file using the ‘src’ attribute of a script tag.
Ex:refer to the program in page no.93(done in lab)
4. What are functions in JavaScript?
[Link] in JavaScript are blocks of reusable codes that
performs specific [Link] are used to organise and simplify our
code.A function starts with the keyword function.
There are two types of functions in Javascript
Built in functions
User defined functions
5. What is JavaScript used for?List any two features of JavaScript.
[Link] is one of the most popular client-side scripting
[Link] code written in JavaScript is referred to as ‘Script’.These
scripts allow you to modify elements on a webpage based on the actions
taken by the user,making the webpage [Link] features of JS are
(choose any two features from page no.91)
[Link] between alert() and prompt() functions.
[Link]() and prompt() functions are the two built in functions in
[Link] alert() function is used to display a dialog box with a
message and an ok [Link] is a simple way to show a message to the
[Link] Prompt() function is used to display a dialog box with a
message and an input field where the user can enter [Link] prompt() is
used if you want the user to input a value.(See the example programs in
the [Link] 95 and 96)
7. What is Document Object Model?Explain any three DOM
methods.
[Link] DOM is a programming interface that allows javascript to interact
with the HTML [Link] DOM is a built in function used to do
specific operations and action on HTML [Link] of the DOM
methods are:
[Link]()-used to select an element by its
[Link] elements by their id is a common and efficient way
to access specific elements in the [Link] HTML element can
be given a unique value(id) for its id attribute,which allows you to
uniquely identify that element using javascript.
Refer to example 1,example2 and example3 programs in page
no.101 and 102 in text book.
[Link]()-This method is used
to select and modify elements by their class [Link] returns a
collection of elements as an [Link] to example program in
page no.104.
[Link]()-
The method [Link]() is used to
select all elements in the HTML file with a specified [Link]
searches the entire document for all elements with the given tag
name and returns them as a collection.
Refer to the example program in page no.105 that counts the
number of <h2> tags present in an HTML document.