<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Taevas</title>
    <description>The latest articles on DEV Community by Taevas (@taevas).</description>
    <link>https://dev.to/taevas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F848213%2F6249eaff-b2b6-47b9-9924-6f2b1943347a.png</url>
      <title>DEV Community: Taevas</title>
      <link>https://dev.to/taevas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taevas"/>
    <language>en</language>
    <item>
      <title>XMLHttpRequest - An amazing object for updating a webpage without reloading</title>
      <dc:creator>Taevas</dc:creator>
      <pubDate>Sun, 17 Apr 2022 13:37:39 +0000</pubDate>
      <link>https://dev.to/taevas/xmlhttprequest-an-amazing-object-for-updating-a-webpage-without-reloading-58h4</link>
      <guid>https://dev.to/taevas/xmlhttprequest-an-amazing-object-for-updating-a-webpage-without-reloading-58h4</guid>
      <description>&lt;p&gt;Let's say you wanna make a simple webpage, where the user can click on a single button to send in the background a request to an API (your website's API or a &lt;a href="https://github.com/public-apis/public-apis"&gt;publicly available API&lt;/a&gt;, and once a response to that request has been received, display it in a nice way on the webpage&lt;/p&gt;

&lt;p&gt;Well, this is all possible from the client-side, thanks to &lt;strong&gt;XMLHttpRequest&lt;/strong&gt;!&lt;/p&gt;




&lt;h2&gt;
  
  
  Choosing the API and the webpage's purpose
&lt;/h2&gt;

&lt;p&gt;For this example, I will be using &lt;a href="https://waifu.im/docs/"&gt;waifu.im&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The webpage should be able to display a random image provided by this API (using its &lt;code&gt;/random/&lt;/code&gt; path) whenever the user clicks a certain button&lt;/p&gt;

&lt;p&gt;That image should fit the parameters specified by the user, using two radios (&lt;code&gt;&amp;lt;input type="radio"&amp;gt;&lt;/code&gt;)&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating the HTML file
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;meta charset="utf-8"&amp;gt;
        &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1"&amp;gt;
        &amp;lt;title&amp;gt;getWaifu&amp;lt;/title&amp;gt;
        &amp;lt;script type="text/javascript" src="index.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;

        &amp;lt;!-- 1st part: Allow the user to specify what sort of image they want --&amp;gt;
        &amp;lt;div id="parameters"&amp;gt;
            &amp;lt;div id="img_type" class="parameter"&amp;gt;
                &amp;lt;input type="radio" id="png_jpg" name="img_type" value="PNG_JPG" checked&amp;gt;
                &amp;lt;label for="png_jpg"&amp;gt;Not Animated (PNG/JPG)&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
                &amp;lt;input type="radio" id="gif" name="img_type" value="GIF"&amp;gt;
                &amp;lt;label for="gif"&amp;gt;Animated (GIF)&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div id="safe" class="parameter"&amp;gt;
                &amp;lt;input type="radio" id="sfw" name="safe" value="SFW" checked&amp;gt;
                &amp;lt;label for="sfw"&amp;gt;Safe For Work (SFW)&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
                &amp;lt;input type="radio" id="nsfw" name="safe" value="NSFW"&amp;gt;
                &amp;lt;label for="nsfw"&amp;gt;Not Safe For Work (NSFW)&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;!-- 2nd part: Give the user an image that matches the parameters --&amp;gt;
        &amp;lt;button id="trigger" onclick="waifu('random', document.getElementById('parameters'), document.getElementById('display'))"&amp;gt;Get a Waifu!&amp;lt;/button&amp;gt;

        &amp;lt;!-- 3rd part: Create a "home" or "container" for the image --&amp;gt;
        &amp;lt;div id="display"&amp;gt;
            &amp;lt;span style="display: none"&amp;gt;Placeholder~&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;

    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;1st part&lt;/strong&gt; of the body holds the parameters: the user decides whether or not the image should be animated, and whether or not it should be "safe for work"&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;2nd part&lt;/strong&gt; of the body is a single button, calling a function (&lt;code&gt;waifu()&lt;/code&gt;) specified in our future JS file upon clicking on it, with the string &lt;code&gt;"random"&lt;/code&gt; as well as the 1st and 3rd parts of the body as arguments&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;3rd part&lt;/strong&gt; of the body is where we want the image to appear, replacing the invisible &lt;code&gt;span&lt;/code&gt; placeholder in the process&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating the JS file
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function waifu(route, params, display) {

    // Figure out what are the specified parameters
    let inputs = params.getElementsByTagName("input")
    let gif = inputs.gif.checked
    let nsfw = inputs.nsfw.checked

    // Create the request
    let xhr = new XMLHttpRequest()
    xhr.open("GET", `https://api.waifu.im/${route}?is_nsfw=${nsfw}&amp;amp;gif=${gif}`)
    xhr.setRequestHeader("Accept", "application/json")

    // Specify the behaviour upon receiving a response
    xhr.onreadystatechange = () =&amp;gt; {
        if (xhr.readyState !== 4) return
        let data = JSON.parse(xhr.response).images[0]

        // Create the image element
        let image = document.createElement("img")
        image.setAttribute("src", data.url)
        image.setAttribute("alt", "Couldn't load the image... &amp;gt;&amp;lt;")

        // Put it on the website
        display.replaceChild(image, display.lastElementChild)
    }

    // Send the request
    xhr.send()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, we deal with the &lt;code&gt;params&lt;/code&gt; object, which is the second object specified by the button, in order to determine if the image the user wants should be a gif or/and if it should be NSFW&lt;/p&gt;

&lt;p&gt;Once that is done, we have everything needed to build the request, so we create a new request with &lt;strong&gt;XMLHttpRequest&lt;/strong&gt; and we give it the name &lt;code&gt;xhr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;request's URL&lt;/strong&gt;, &lt;code&gt;route&lt;/code&gt; will always be &lt;code&gt;"random"&lt;/code&gt; because the only button calling that function specifies that&lt;/p&gt;

&lt;p&gt;We're doing a simple &lt;strong&gt;GET request&lt;/strong&gt;, and we expect the response's data to be in &lt;strong&gt;JSON format&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then, we specify what should happen when the status of the request changes, the &lt;code&gt;if (xhr.readyState !== 4) return&lt;/code&gt; line means that the code below it should not be executed unless a response has been received&lt;/p&gt;

&lt;p&gt;In the code below, we create an &lt;code&gt;img&lt;/code&gt; element using a property received in the response (&lt;code&gt;data.url&lt;/code&gt;) and we put that element on the website using &lt;code&gt;display&lt;/code&gt;, by replacing whatever's in it! (the placeholder if it is the first time the function is called)&lt;/p&gt;

&lt;p&gt;Note that &lt;strong&gt;this code only uses 1 of many properties&lt;/strong&gt; &lt;code&gt;data&lt;/code&gt; holds, it has a lot more, which allows you to show lots of details about the image!&lt;/p&gt;

&lt;p&gt;Now that all the details of the request have been specified, &lt;strong&gt;the request can be sent&lt;/strong&gt; using &lt;code&gt;xhr.send()&lt;/code&gt;, and the magic will happen!&lt;/p&gt;




&lt;h2&gt;
  
  
  Seeing the result
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Note: A live example of all the code above (with added CSS) can be found &lt;a href="https://static.taevas.xyz/getWaifu/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that we're all done, you can open your HTML file in your browser, open up its network monitor (CTRL+Shift+E on Firefox, CTRL+Shift+J then click on "Network" for any Chromium-based browser) and click the button to get an image on the webpage!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--86l1ORZv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydlbburcpo9o0kqqjed4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--86l1ORZv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydlbburcpo9o0kqqjed4.png" alt="How it looks on Firefox" width="485" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you see the last two requests? &lt;strong&gt;The first request is to the API&lt;/strong&gt;, in order to get &lt;code&gt;data&lt;/code&gt; and to modify your webpage with it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The second request is the one actually getting the image&lt;/strong&gt;; The first request had only gotten the URL of the image to create an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element, but now that this element is on the webpage, it needs to be downloaded from its &lt;code&gt;src&lt;/code&gt; attribute! Of course, its &lt;code&gt;src&lt;/code&gt; attribute is the URL gotten from the first request&lt;/p&gt;




&lt;p&gt;I believe this covers pretty much everything!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XMLHttpRequest&lt;/strong&gt; is truly crazy, as it allows you to make the user request and receive any data, anywhen (not only upon clicking anything!) and to do anything with that data, including changing the user's webpage, to its integrity if you wish it so!&lt;/p&gt;

&lt;p&gt;I hope you have fun imagining what you can do with this, and even more fun actually making it work!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>html</category>
    </item>
  </channel>
</rss>
