Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>AJAX One</title>
- <style rel="stylesheet" type="text/css">
- .game-changer {padding: 10px 0;}
- </style>
- <script type="text/javascript">
- // This code works just fine on Edge and Mozilla, in order to work on Chrome you need to disable CORS on Chrome,
- // by typing the following command in command prompt: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="c:/chromedev OR you install CORS extension on your Chrome browser.
- // If you work on Chrome besides disabling CORS don't check if request status is 200, because for requesting local files it will
- // always return 0 so if statement will always fail, although for requesting online files it will still return 200.
- function get(success) {
- var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
- request.open("GET", "plain_text.txt");
- request.onreadystatechange = function() {
- if(this.readyState === 4 && this.status === 200)
- success(this.responseText);
- }
- request.send();
- }
- document.addEventListener("DOMContentLoaded", function() {
- document.getElementsByClassName("change")[0].addEventListener("click", function() {
- get(function(data) {
- document.getElementsByClassName("game-changer")[0].innerHTML = data;
- })
- });
- });
- </script>
- </head>
- <body>
- <div class="game-changer">
- <h1>Let AJAX change this text</h1>
- </div>
- <button class="change">Change Content</button>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment