"Js" Examples
JavaScript
Examples:
1.
Alert Message: `JavaScript` is often used to create interactive web pages with alert messages. For example:
html
2.
Dynamic Updates: `JavaScript` can update web page content dynamically. For instance:
javascript
var para document.getElementById("my_para");
para.innerHTML "JavaScript can also be used to update dynamic content";
3.
Form Validation: `JavaScript` helps validate user input in web forms. Here's an example:
javascript
function validateForm() {
var x document.forms["myForm"]["name"].value;
if (x "") {
alert("Name must be filled out");
return false;
}
}
4.
Animation and Graphics: `JavaScript` can create interactive animations and graphics. For example:
javascript
function animate() {
var c document.getElementById("myCanvas");
var ctx c.getContext("2d");
ctx.fillStyle "rgba(255,0,0,0.5)";
ctx.fillRect(0,0,150,75);
setTimeout(animate, 100);
}
5.
Client-Side Calculations: `JavaScript` can perform calculations and data processing on the client-side. For instance:
javascript
function calculateTip() {
var bill parseFloat(document.getElementById("bill").value);
var tip bill 0.1;
document.getElementById("tip").value tip;
}
These examples illustrate the versatility of `JavaScript` in creating interactive web applications.