How to Program in Ajax
Method 1 of 2:
Coding
- Prepare a picture for writing an Ajax program. Save the picture in the same folder where you will save your html and text files displaying the Ajax program. In this article, 'ProgramInAjax' directory is set up inside the 'wamp' folder under the 'www' directory where you installed WampServer.
- Open any text editor. Notepad++ is used as the text editor in this article.
- Create a new file in the text editor. Type the following:
Oh oh! Where did the yellow flower go?
You may type whatever you want inside the html tag - Save the file as a text document with the name of 'ajax-data.txt.' Actually, you can name the file whatever you want but make sure you enter the same file name to the coding in this line:
xmlhttp.open("GET","ajax-data.txt",true);
However, the HTML tag - Create a new file for a webpage. This file is for an HTML file to view the Ajax program in a Web browser.
- Copy the following code:
<html> <head> <title>My First Ajax Program by Metitle> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("myImage").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET","ajax-data.txt",true); xmlhttp.send(); } script> head> <body style="text-align: center;"> <div id="myImage"> <h2>Click the button below to make the flower disappear.h2> <img src="MyPicture.png" width="500px" height="300px" title="Yellow Flower" alt="an image of a yellow flower"/> div> <br/> <button type="button" onclick="loadXMLDoc()">Click here to make the picture disappear!button> body> html>
- Save the file. Click the save button on the menu bar. A 'Save As' box is open. Enter a name for your document. In this article, the name of the file is 'index.'
- Click the drop down arrow to choose the file extension. At the 'Save as type' field, click the drop down arrow to choose the file extension.
- Select 'Hyper Text Markup Language file.' Make sure that it has 'html' inside the parenthesis. Click save after selecting the 'html.'
- Test the HTML file in a Web browser. Open the webpage in a web browser. Go to 'Run' on the top menu bar. Click it and select 'Launch in Chrome' or any browser being installed in your system. Google Chrome is used for the testing in this article. You may have some other browsers installed within Notepad++. You can select your favorite browser. Another option, you can click the WampServer icon at the taskbars at the bottom of the screen and select 'Localhost.' You should see your directory there and click the index file.
- Click the button below the picture to test the script.
- Your final webpage. Your webpage should be refreshed with the information that you entered into the text file at the beginning. The flower and the header should be replaced with the new header called 'Oh oh! Where did the yellow flower go?'
Method 2 of 2:
Code Explanation
- The body section. The body of HTML has the 'div' section and one button. This section will be used to display the information returned from the server. The button calls a function named 'loadXMLDoc(),' if it is clicked.
DOCTYPE html> <html> <head> <title>My First Ajax Program by Me</title> </head> <body style="text-align: center;"> <div id="myImage"> <h2>Click the button below to make the flower disappear.</h2> <img src="MyPicture.png" width="500px" height="300px" title="Yellow Flower" alt="an image of a yellow flower"/> </div> <br/> <button type="button" onclick="loadXMLDoc()">Click here to make the picture disappear!</button> </body> </html>
- The head section. The head section of the HTML file has a script tag which contains the 'loadXMLDoc()' function.
<head> <title>My First Ajax Program by Me</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("myImage").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET","ajax-data.txt",true); xmlhttp.send(); } </script> </head>
- More Explanation. The most important thing of Ajax is the XMLHttpRequest object. It is used to exchange data with the server and all modern browsers support the object.
- The syntax to create an XMLHttpRequest() object is variable=new XMLHttpRequest(); but at the same time the syntax to create old versions of Internet Explorer (IE5 and IE6) that uses an ActiveX object is variable=new ActiveXObject("Microsoft.XMLHTTP");.
- In order to handle all the modern browsers, it needs to check if the browsers support the XMLHttpRequest object. If it does, it creates an XMLHttpRequest object. If one does not, it will create an ActiveX object for it.
- Then it will send a request to the server. The method of the XMLHttpRequest object called 'open()' and 'send()' will be used. xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send();.
4 ★ | 1 Vote
You should read it
May be interested
- How to uninstall Windows Insider Program Windows 10windows 10 windows insider program is a program registered with those who want to experience before the preview will be upgraded to the full version before it is released.
- Learn about the Program files folder in Windowsprogram files is a folder first found in microsoft windows 95 and included in all later versions of microsoft windows, for storing software programs installed on a computer.
- What is a sandbox and how does it sandbox a program?many browsers today are designed to automatically run in their own sandbox without user settings.
- Instructions for uninstalling Windows Insider Programthe windows insider program allows you to experience new features early, but it comes with the risk of system errors and instability. if you no longer want to use this test build, exiting the program is necessary to ensure performance and stability of your computer.
- Microsoft rewarded $ 250,000 in a new bug-finding programmicrosoft finally launched a bug bounty program for security researchers and bug hunters, and then reported bugs on the latest windows operating systems and software.
- How to Run a Program on Command Promptthis wikihow teaches you how to start a program on your windows computer from within the command prompt app. you can only run programs that are installed in windows-created folders (e.g., the desktop), though you can add a program's folder...
- How to Open a File with Another Program Using Open With on Windows Vistado you have a file on your computer which seems to always open in another program that you don't want to use? although the program is on your system, it's just not accessing it for either this instance or for all instances. this article...
- Basic C program structurebefore we study the blocks that make up a c program, first look at a sample c program.
- 8 tools to hide programs running on Taskbar or Traybarif you want to hide a program or application running on the taskbar for some reason, you don't know how. the network administrator will introduce you to 8 useful tools to help you hide the program in the taskbar.
- ADX Toys 2: Programming editor for Wordprogramming requires you to have a suitable program and a specific environment, plus a translation program to translate the source program. however, when you want to interpret a certain meaning content in a language, you need to write it into a text to express your meaning.