Skip to main content

Posts

Showing posts with the label script

Simple Validation in JavaScript:

Javascript can have simple validations and such an example is given below. Download and use the code. <html> <head> </head> <body> Input numbers between 0 and 5 Submit </body> </html> functions.js function validate() { var x, text; x = document.getElementById("numb").value; if(isNaN(x) || x < 1 || x > 5) { text = "Input not valid"; }else { text = "Input OK"; } document.getElementById("show").innerHTML = text; }

Dialog Boxes in JavaScript

Alert Dialog Box: An alert dialog box is mostly used to give a warning message to the users.Mostly they gives only an "Ok button". Confirmation Dialog Box: A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons mostly "ok" and "cancel" Prompt Dialog Box: The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus, it enables you to interact with the user. The user needs to fill in the field and then click OK. If the user clicks the Cancel button, the window method prompt() returns null. <html> <head> </head> <body> Show an Alert Dialog Box Show alert Show a Confirmation Dialog Box Show alert Prompt Dialog Box </body> </html> functions.js: function showalert1(){ alert("Iam an alert") } function showalert2(){ confirm("Iam an alert") } function getValue() ...

Calling a script from an external file

We Can just recall the previous example of image switching to understand how to call script externally.  Let's have a look. <html> <head> </head> <body> click on the image </body> </html> Using the tag <script type="text/javascript" src="functions.js"> </script> we can call the script externally. Always write script in file with extension ".js". functions.js: function imageswitch(){ var image = document.getElementById('switchimage'); if(image.src.match("one")) { image.src = "flower_two.jpg"; } else { image.src = "flower_one.jpg"; } } Note: Make sure that the both html and js file locations are the same or else defime them specifically

Alert, Confirm, and Prompt boxes in Javascript

The three "commands" involved in creating alert, confirm, and prompt boxes are: window.alert() window.confirm() window.prompt() window.alert() : This command pops up a message box displaying whatever you put in it. <body> <script type="text/javascript"> window.alert("Iam an alert box!!!") </script> </body> window.confirm(): Confirm is used to confirm a user about certain action, and decide between two choices depending on what the user chooses. <body> <script type="text/javascript"> var x=window.confirm("Are you sure?") if (x) window.alert("Yes!") else window.alert("No") </script> </body> window.prompt(): Prompt is used to allow a user to enter something, and do something with that info: <body> <script type="text/javascript"> var y=window.prompt("Enter your name here...") window.alert(y) </script> </body> ...

HTML Tag - Scripting

<script> This tag is used to define a client-side script, such as a javascript. <noscript> This tag is used to define an alternate content for users that have disabled scripts in their browser or have a browser that doesn't support script.The <noscript> can be used in both <head> and <body>.