Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Java Script

The document discusses JavaScript, including that it is a lightweight, dynamic programming language commonly used in web pages to provide interactivity. It describes how JavaScript code can be included in HTML documents and some basic JavaScript concepts like functions, loops, conditional statements, arrays, and events.

Uploaded by

praveshcode1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Java Script

The document discusses JavaScript, including that it is a lightweight, dynamic programming language commonly used in web pages to provide interactivity. It describes how JavaScript code can be included in HTML documents and some basic JavaScript concepts like functions, loops, conditional statements, arrays, and events.

Uploaded by

praveshcode1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA SCRIPT

javascript is a dynamic computer programming language. It is lightweight and most commonly


used as a part of web pages, whose implementations allow client-side script to interact with the
user and make dynamic pages. It is an interpreted programming language with object-oriented
capabilities. Client-side JavaScript is the most common form of the language. The script should
be included in or referenced by an HTML document for the code to be interpreted by the
browser. It means that a web page need not be a static HTML, but can include programs that
interact with the user, control the browser, and dynamically create HTML content.

Advantages of JavaScript

-> Less server interaction: You can validate user input before sending the page off to the server.
This saves server traffic, which means less load on your server.
-> Immediate feedback to the visitors: They don't have to wait for a page reload to see if they
have forgotten to enter something.
-> Increased interactivity: You can create interfaces that react when the user hovers over them
with a mouse or activates them via the keyboard.
-> Richer interfaces: You can use JavaScript to include such items as drag-anddrop components
and sliders to give a Rich Interface to your site visitors.

Limitations of JavaScript

We cannot treat JavaScript as a full-fledged programming language. It lacks the following


important features:
1. Client-side JavaScript does not allow the reading or writing of files. This has been kept for
security reason.
2. JavaScript cannot be used for networking applications because there is no such support
available.
3. JavaScript doesn't have any multithreading or multiprocessor capabilities.

JavaScript can be implemented using JavaScript statements that are placed within

the <script>... </script> HTML tags in a web page.


The script tag takes two important attributes:

1. Language: This attribute specifies what scripting language you are using. Typically, its value
will be javascript. Although recent versions of HTML (and XHTML, its successor) have phased
out the use of this attribute.
2. Type: This attribute is what is now recommended to indicate the scripting language in use and
its value should be set to "text/javascript".

JavaScript in External File


<html>
<head>
<script type="text/javascript" src="filename.js" ></script>
</head>
<body>
.......
</body>
</html>

Internal Java Script


<html>
<head>
</head>
<body>
<script>
document.write("<h1>welcome</h1>");
</script>
</body>
</html>
Loops in java script
i) For
ii) While
iii) Do while

Conditional statements
i) If else
ii) switch

Functions in java

A JavaScript function is defined with the function keyword, followed by a name, followed by
parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables). The parentheses may include parameter names separated by commas: (parameter1,
parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
var x = myFunction(4, 3); // Function is called, return value will end up in x

function myFunction(a, b)
{
return a * b; // Function returns the product of a and b
}

Pop up Box:

Alert Box
An alert box is often used if you want to make sure information comes through to the user. When
an alert box pops up, the user will have to click "OK" to proceed.

Syntax
window.alert("sometext");

Confirm Box
A confirm box is often used if you want the user to verify or accept something. When a confirm
box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks
"OK", the box returns true. If the user clicks "Cancel", the box returns false.

if (confirm("Press a button!")) {
txt = "You pressed OK!";
}
else {
txt = "You pressed Cancel!";
}

Prompt Box
A prompt box is often used if you want the user to input a value before entering a page. When a
prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering
an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel"
the box returns null.
Syntax
window.prompt("sometext","defaultText");
var person = prompt("Please enter your name", "Harry Potter");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}

Events

<button onclick="document.getElementById('demo').innerHTML = Date()">The time


is?</button>

Array
var cars = ["Saab", "Volvo", "BMW"];

<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>

var cars = ["Saab", "Volvo", "BMW"];


document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>

getElementById

The getElementById() method returns the element that has the ID attribute with the specified
value.
This method is one of the most common methods in the HTML DOM, and is used almost every
time you want to manipulate, or get info from, an element on your document. Returns null if no
elements with the specified ID exists.

<html>
<body>
<p id="demo">Click the button to change the text in this paragraph.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>

getElementsByName()

<html>
<body>
First Name: <input name="fname" type="text" value="Michael"><br>
First Name: <input name="fname" type="text" value="Doug">
<p>Click the button to get the tag name of the first element in the document that
has a name attribute with the value "fname".</p>

<button onclick="myFunction()">Try it</button>


<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByName("fname")[0].tagName;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

You might also like