LonelyShepherd

AJAX Change [Лаб. 6.1]

Dec 26th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <title>AJAX One</title>
  6.         <style rel="stylesheet" type="text/css">
  7.             .game-changer {padding: 10px 0;}
  8.         </style>
  9.         <script type="text/javascript">
  10.             // This code works just fine on Edge and Mozilla, in order to work on Chrome you need to disable CORS on Chrome,
  11.             // 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.
  12.             // If you work on Chrome besides disabling CORS don't check if request status is 200, because for requesting local files it will
  13.             // always return 0 so if statement will always fail, although for requesting online files it will still return 200.
  14.            
  15.             function get(success) {
  16.                 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
  17.                 request.open("GET", "plain_text.txt");
  18.                 request.onreadystatechange = function() {
  19.                     if(this.readyState === 4 && this.status === 200)
  20.                         success(this.responseText);
  21.                 }
  22.  
  23.                 request.send();
  24.             }
  25.            
  26.             document.addEventListener("DOMContentLoaded", function() {
  27.                 document.getElementsByClassName("change")[0].addEventListener("click", function() {
  28.                     get(function(data) {
  29.                         document.getElementsByClassName("game-changer")[0].innerHTML = data;
  30.                     })
  31.                 });
  32.             });
  33.         </script>
  34.     </head>
  35.     <body>
  36.         <div class="game-changer">
  37.             <h1>Let AJAX change this text</h1>
  38.         </div>
  39.         <button class="change">Change Content</button>
  40.     </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment