Skip to main content

Posts

Showing posts from December, 2012

Using Ajax to Query MySQL

Using Ajax to Query MySQL Creating the MySQL Table: CREATE TABLE `ajax_example` ( `name` varchar(50) NOT NULL, `age` int(11) NOT NULL, `sex` varchar(1) NOT NULL, `wpm` int(11) NOT NULL, PRIMARY KEY (`name`) ); Inserting Data into the Table: INSERT INTO `ajax_example` VALUES ('Jerry', 120, 'm', 20); INSERT INTO `ajax_example` VALUES ('Regis', 75, 'm', 44); INSERT INTO `ajax_example` VALUES ('Frank', 45, 'm', 87); INSERT INTO `ajax_example` VALUES ('Jill', 22, 'f', 72); INSERT INTO `ajax_example` VALUES ('Tracy', 27, 'f', 0); INSERT INTO `ajax_example` VALUES ('Julie', 35, 'f', 90); Client Side HTML File (ajax.html): <!DOCTYPE html> <html> <head> <title>Ajax Example</title> <script> function ajaxFunction() { var ajaxRequest; try { ajaxRequest = new XMLHttpRequest(); } catch (e)...

Using JavaScript Image Map

JavaScript can be used to create client-side image maps, which are defined using the <map> and <area> tags in conjunction with the usemap attribute of the <img /> tag. The <map> element acts as a container for defining clickable hotspots within an image. Each <area> tag specifies a shape and coordinates for the hotspot. Below is an example demonstrating how to use an image map with JavaScript to display different messages when hovering over specific areas of an image: <html> <head> <title>Using JavaScript Image Map</title> <script type="text/javascript"> function showTutorial(name) { document.myform.stage.value = name; } </script> </head> <body> <form name="myform"> <input type="text" name="stage" size="20" /> </form> <img src=...

Page Redirection Examples - Learn How to Redirect Web Pages

Page redirection is essential for directing users from one URL to another. Learn how to implement different types of redirects using JavaScript: Why Use Page Redirection? Migrating to a new domain without losing traffic. Routing users based on browser types or geographical locations. Maintaining SEO when restructuring URLs. Examples of Page Redirection Example 1: Instant Redirection Redirect users immediately: <script type="text/javascript"> window.location = "http://www.newlocation.com"; </script> Example 2: Delayed Redirection with Message Display a message and redirect after a delay: <script type="text/javascript"> function Redirect() { window.location = "http://www.newlocation.com"; } document.write("Redirecting in 10 seconds..."); setTimeout('Redirect()...

Printing Web Pages with JavaScript - Learn How to Implement Print Functionality

Implementing printing functionality on web pages using JavaScript's window.print() function allows users to print the current page directly from the browser. Implementing Print Functionality To enable printing, simply use a button with an onclick event: <input type="button" value="Print" onclick="window.print()" /> This button, when clicked, triggers the browser's print dialog. Making a Page Printer-Friendly A printer-friendly page is optimized for printing, usually with minimal graphics and ads: Create a separate printer-friendly version of the page without unnecessary elements. Alternatively, mark printable content with HTML comments like <!-- PRINT STARTS HERE --> and <!-- PRINT ENDS HERE --> , and use server-side scripts to generate a printable version. Our site follows these practices to provide a better printing e...

Client-Side Form Validation with JavaScript

In traditional web forms, validation occurred on the server after submission, leading to delays and server load. JavaScript allows for client-side form validation, checking data before it's sent to the server. Basic Form Validation Basic validation ensures required fields are filled and formats are correct: <form action="/cgi-bin/test.cgi" name="myForm" onsubmit="return validate()"> <input type="text" name="Name" /> <input type="text" name="EMail" /> <input type="text" name="Zip" /> <select name="Country"> <option value="-1" selected>[choose yours]</option> <option value="1">USA</option> <option value="2">UK</option> <option value="3">INDIA</option> </select> <input type="submit" val...

Types of Errors and Error Handling in JavaScript

Errors in programming can be categorized into three types: Syntax Errors, Runtime Errors, and Logical Errors. Syntax Errors Syntax errors, also known as parsing errors, occur during compilation or interpretation: <script type="text/javascript"> window.print(; // Syntax error: missing closing parenthesis </script> When a syntax error occurs in JavaScript, only the affected thread stops, allowing other threads to continue. Runtime Errors Runtime errors, or exceptions, occur during script execution: <script type="text/javascript"> window.printme(); // Runtime error: calling a non-existing function </script> Exceptions affect the thread they occur in, but other JavaScript threads can continue normally. Logical Errors Logical errors are mistakes in the logic of your script, resulting in unexpected behavior: These errors are not sy...