JavaScript Language by VJTech Academy_removed (1)
JavaScript Language by VJTech Academy_removed (1)
- In JavaScript, functions are normally not called directly from the top level of JavaScript
program.
- Functions are called in response to various user action, such as clicking a button, move
mouse over the elements, etc.
Events
- JavaScript code is executed on web page thorugh event.
- Events means action performed by the user.
- JavaScript enabled web pages are event driven. Events are actions that occur on the
webpage.
- Events are signals generated when specific action occurs.
- Events that might occur are due to following reasons:
1) A document loading
2) The user clicking mouse button.
3) The browser screen changing size. etc.
Form Events
Event Description
Occurs when a form is submitted, either by clicking a submit button
submit
or pressing Enter.
reset Occurs when a form is reset using a reset button.
Occurs when the value of a form element (input, select, etc.)
change
changes and loses focus.
input Occurs when the value of an input element changes.
Occurs when an element gains focus, such as when a user clicks or
focus
tabs into an input field.
Occurs when an element loses focus, such as when a user clicks
blur
outside an input field.
6.1 Status bar – build a static message, changing the message using rollover,
moving the message along the status bar.
6.2 Banner – loading and displaying banner advertisement. Linking a banner
advertisement to URL.
6.3 Slide Show – Create a slide show
6.4 Menus – Creating a pulldown menu, dynamically changing a menu,
validating menu selection, Floating menu, folding, chain select menu, tab
menu, pop-up menu, sliding menu, highlighted menu, folding a tree menu,
context menu, scrollable menu, sidebar menu.
6.5 Protecting Web Page – Hiding your code, disabling the right mouse button,
JavaScript, concealing email address.
6.6 Frameworks of JavaScript and Its applications.
Status Bar :
- The JavaScript status bar is a thin horizontal bar at the bottom of the browser window that
can be used to display information to the user.
- It is typically used to display the status of a loading page, the filename of a linked file, or the
coordinates of the mouse cursor.
- The status
us bar is accessed through the window.status property.
- To set the text in the status bar, simply assign a string to the window.status property.
- For example, the following code will set the text in the status bar to “Done”.
window.status = "Done";
- However,
ever, it's important to note that modern web browsers have limited the ability to interact
with or modify the status bar for security and usability reasons.
Examples :
<html>
<head>
<title>Changing Status Bar On Rollover</title>
</head>
<body>
<h1>Change status message using rollover</h1>
<a onmouseover="window.status='welcome to status bar..This is C Lang'">
<h2>C Language</h2>
</a>
<a onmouseover="window.status='welcome to status bar..This is C++ Lang'">
<h2>C++ Language</h2>
</a>
</body>
</html>
<html>
<head>
<script language="javascript" type="text/javascript">
var scrollpos = 0;
var maxscroll = 100;
var blanks = "";
function scrollText(text) {
window.setInterval(displayText(text), 500);
}
function displayText(text) {
window.status = blanks + text;
++scrollpos;
blanks += " ";
if (scrollpos > maxscroll) {
101 > 100;
scrollpos = 0;
blanks = "";
}
}
</script>
</head>
<body>
<input type="button" name="b1" value="Status Bar"
onclick="scrollText('Welcome to status bar')"/>
</body>
</html>
Banner :
- A rectangular area in webpage where images is placed for advertisement purpose is known
as Banner.
- Banner can be placed anywhere in the webpage, horizontally on top and bottom or vertically
at right and left side of the webpage.
- Too load and display the banner advertisement, we placed an image is advertisement of
particular advertiser.
- We can also provide the link to that image.
- When image is clicked it will go to the advertiser's website.
- Nearly all banner
nner advertisements are in a ffile
le format such as a GIF, JPG, TIFF,
T or other
common graphic file formats.
- Some are animated GIFs, which is a series of images contained in one file that rotate
automatically on the screen.
Step 1: Create your banner poster in any file format such as “JPG, JPEG, GIF, etc.
Step 2: Create an <img> element in your web page with the height and width necessary to
display the banner advertisement.
Step 3: Add JavaScript code if you want to change banner dynamic etc.
Examples :
1. Simple Banner Example
<html>
<head>
<title>Banner Demo</title>
</head>
<body>
<center>
<h2>This Is A Banner</h2>
<img src="./Banner1.jpg" alt="Banner" width="70%" />
</center>
</body>
</html>
Output :
Output :
Explanation: Steps:
1. Create an array of banner images.
2. Create a variable timerID and initialize it to null.
3. Create a variable i and initialize it to 0.
4. Create a function display() to display the banner images.
5. Check if the timerID is null.
6. If the timerID is null, then set the timerID to setInterval() function.
7. Create a function display_banner() to display the banner images.
8. Check if the value of ‘i’ is less than the length of the banner_array.
9. If the value of i is less than the length of the banner_array, then set the src attribute of the
image to the banner_array[i].
10. Increment the value of i by 1.
11. Else, clear the timerID using clearInterval() function.
Output: When the Display Banners button is clicked, the banner images are displayed
one after the other.
Task For Students: Using given program add links to all separate banners.
Hint : add images inside the anchor tag and array of links, at iteration time change the “href” and
“src” of anchor and image.
Slide Show :
- A slideshow is similar in concept to a banner advertisement in that a slideshow rotates
multiple images on the web page. However, unlike a banner advertisement.
- A slideshow gives the visitor the ability to change the image that's displayed: the visitor can
click the Forward button to see the next image and the Back button to see the previous
image.
- Simple SlideShow Example
<html>
<head><title>SlideShow Example</title></head>
<body>
<center>
<img src="banner1.png" width="500" height="300" id="image1" />
<br /><br /><br /><br />
<input type="button" value="Previous" onclick="SlideShow(-1)" />
<input type="button" value="Next" onclick="SlideShow(1)" />
</center>
<script language="javascript" type="text/javascript">
var banner_array = new Array("banner1.png","banner2.png",
"banner3.png","banner4.png","banner5.png" );
var i = 0;
function SlideShow(status) {
i = i + status;
if (i < 0) {
i = banner_array.length - 1;
}
if (i > banner_array.length - 1) {
i = 0;
}
document.getElementById("image1").src = banner_array[i];
}
</script>
</body>
</html>
Output:
Menus :
- A menu is a graphical user interface element that allows users to make selections from a list
of options. It's a common UI component in web forms and navigation systems.
- Creating A Simple Menu: The <select> tag in HTML is used to create a menu.
- It's typically accompanied by <option> elements to define the menu items.
- The user selects one option from the list.
<select id="myMenu">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
- Here, There are different types of menus are there that are given below :
Pull-Down Menu :
- A "pull-down menu" typically refers to a dropdown menu or select menu in web
development.
- It is a common user interface element used for presenting a list of options from which users
can make a selection.
- Example :
<html>
<head>
<title>Pull Down Menu Example</title>
</head>
<body>
<center>
<h1>Pull-Down-Menu</h1>
<h1>Select Favorite Programming Language</h1>
<select id="select1" onchange="display(this.value)">
<option value="0">Select Language</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Java">Java</option>
<option value="Python">Python</option>
<option value="PHP">PHP</option>
<option value="JavaScript">JavaScript</option>
</select>
</center>
<script language="javascript" type="text/javascript">
function display(value) {
alert("You have selected " + value);
}
</script>
</body>
</html>
Output:
<body>
<b>Select Programming Type </b>
<select id="myselect" onchange="display(this)">
<option value="category">Category</option>
<option value="POP">POP</option>
<option value="OOP">OOP</option>
</select>
<hr />
<b>Languages </b>
<select id="lang">
<option value="Language">Language</option>
</select>
</body>
</html>
Output:
Floating Menu :
- The JavaScript allows t create dynamic menus which move along with scrolling. Such
Floating menu will be always visible on the screen. They Appear to “float” on the top of the
page.
- In this example, we use a <select> element as the menu, and its options represent different
Programming Languages.
<html>
<head>
<script language="javascript" type="text/javascript">
function Display(m) {
var x = m.options[m.selectedIndex].value;
alert("You have selected language:" + x);
}
</script>
</head>
<body>
<form name="form1" style="position: fixed">
Select Your Favorite Language:
<select onchange="Display(this)" style="height: 25px; width: 160px">
<option value="C">C lang</option>
<option value="C++">C++ lang</option>
<option value="Java">Java lang</option>
<option value="Python">Python lang</option>
</select>
</form>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<hr /><h1>Part 1</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<hr /><h1>Part 2</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<hr /><h1>Part 3</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<hr /><h1>Part 4</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</body>
</html>
- In This Example, CSS is used to position the <select> element. By setting its position
property to "fixed", And optional CSS is adjusting the top and left values, the menu remains
fixed at the top of the page.
Output:
Validating Menu :
- Validating a menu selection is important to ensure that the user's choice is appropriate and
matches the expected values. This validation process helps maintain data integrity and
prevents errors in the application. Here's how you can validate a menu selection:
- Data validation: When the user makes a selection from a menu, especially in forms, it's
essential to validate the selected value. Ensure that the selected option is within the expected
range of values and matches the pr
predefined
edefined options. This step prevents unexpected or
malicious data from being submitted.
- Error Handling: If the selected value does not match any of the predefined options,
appropriate error handling mechanisms should be in place. This can include displaying
displayin an
error message to the user, highlighting the menu with a different color, or preventing form
submission until a valid selection is made.
- Server-Side Validation: While client
client-side
side validation is useful for enhancing user experience,
always perform validation
tion on the server
server-side as well. Client-side
side validation can be bypassed,
so server-side
side validation acts as a safety net to ensure data integrity and security.
option.text = cities[i];
option.value = cities[i];
city.appendChild(option);
}
}
}
</script>
</body>
</html>
- Chained select menus create a hierarchical relationship between options. The selection made
in the first menu (the "parent" menu) determines the available choices in the subsequent
menus (the "child" menus). This dependency ensures that users choose valid combinations of
options.
Tab Menu :
- A tab menu is a common user interface component used to organize content into different
sections or categories, often found in web applications and websites.
- It allows users to switch between sections of content by clicking on tabs.
- Code For Tab Menu :
<html >
<head>
<title>Tab Menu Example</title>
</head>
<body>
<div class="tab" style="margin: 10px 0">
<button onclick="displayData(event, 'Java')"> Java </button>
<button onclick="displayData(event, 'Python')"> Python </button>
<button onclick="displayData(event, 'C++')">C++</button>
<button onclick="displayData(event, 'PHP')">PHP</button>
</div>
<div id="display_content" style="height: 300px; width: 400px; border: 1px
solid; padding: 10px" ></div>
Output:
Pop-Up Menu :
- A pop-up menu, also known as a context menu or right-click menu, is a menu that appears
when a user interacts with a specific element, typically by right-clicking it.
- Pop-up menus provide context-specific options and actions based on the user's interaction
with the element.
- Typical Use Cases: Pop-up menus are commonly used in applications for performing actions
on items in a list, table rows, or any interactive elements. They offer a convenient and
unobtrusive way to access actions.
- Creating Pop-Up Menus: Pop-up menus can be created using a combination of HTML, CSS,
and JavaScript. The HTML structure includes the menu items, CSS styles control its
appearance, and JavaScript handles event listeners and interactions.
Sliding Menu :
- A sliding menu is a user interface component that typically slides into view from the side of
the screen or another hidden location to provide navigation options or additional content..
- Sliding menus are often used in responsive web design, especially for mobile devices, to
save screen space and create an intuitive user experience
experience.
- JavaScript is used to add interactivity. Event listeners are applied to the trigger elements, and
the menu's display is controlled by toggling CSS classes or directly manipulating styles.
- Example :
Protecting Web-Page :
- Protecting a web page is crucial for maintaining the security and integrity of your website,
its data, and user interactions.
- Way To Protect Your Web-Pages
1. Hiding Your Code
Use External JavaScript, CSS files.
Disabling Right (mouse) Click button.
2. User Email Concealing
- Code :
<html>
<head>
<title>Protecting Web Page</title>
<script language="javascript" type="text/javascript">
function display() {
document.addEventListener("contextmenu", function (e) {
alert("Right-click is disabled on this page to protect the content.");
e.preventDefault();
});
}
</script>
</head>
<body>
<input type="button" value="Protect Web Page" onclick="display()" />
</body>
</html>
- Code :
<html>
<head>
<title>Email Concealing Example</title>
</head>
<body>
<p>Enter your email address:</p>
<input type="email" id="tf1" placeholder="Enter Your Email Address" />
<input type="button" onclick="display()" value="Submit" />
<p id="demo"></p>
<script language="javascript" type="text/javascript">
function display() {
var splitted, part1, part2, avg, result;
email = document.getElementById("tf1").value;
//[email protected]
if (email == "") {
alert("Please Enter Email Address!");
return;
}
splitted = email.split("@");
Frameworks of JavaScript :
- JavaScript frameworks are pre-built libraries or collections of tools and functions that
simplify and accelerate web application development.
- They provide a structured and organized way to build interactive, efficient, and scalable web
applications.
Key Advantages :
- Productivity: Frameworks simplify common tasks, such as DOM manipulation, data
handling, and routing, allowing developers to work more efficiently.
- Consistency: Frameworks encourage consistent coding practices and enforce design patterns,
leading to maintainable and readable code.
- Scalability: They are designed to help applications grow, enabling developers to add features
and scale without compromising performance.
Full-Stack Frameworks: These combine both front-end and back-end frameworks into a
cohesive solution. Examples include Meteor and Next.JS.