Open In App

Create a Gif Search Engine Using JavaScript

In this article, we will create a Gif Search Engine using JavaScript. The basic application of Gif search Engine is to search the images from the given user input keywords and the output of the images will be loaded on the same page with different aspect ratios and sizes provided by Giphy. To get the output of the image we will use Giphy EndPoint Search API.  

By means of EndPoint in API, we are actually accessing the Giphy Images Delivering Services using the endpoint in our URL. 



Firstly we will create a HTML layout and style the elements using CSS. The Page contains the input field to  the search keywords for the images and some style it with CSS.

The basic HTML Layout presents the structure of the page and it includes the following-



CSS Style: The created layout of the above HTML and CSS would look like this,

Main Logic of the Search Engine: Firstly, To enable the User Input keyword for searching the Gif images and showing them as output is written in a JavaScript File. There are a bunch of functions and logics to run the search Query.

There are several steps to follow along which would help to get the logic.

Step-1: Getting the Input from the User: Initially the input field have some keyword or value entered by the user which ensures that the logic would be performed on the given keyword. Here the keyword is selected by the query Selector method which then returns the Input keyword or a value. 




function getUserInput() {
  var inputValue = document
    .querySelector(".js-userinput").value;
  return inputValue;
}

Step-2: Getting the Input and Processing the Request: Further, A set of operation is performed when the Search button is clicked. The value returned by the function `getUserInput` is now used by other functions as well. The value is then handled by handler for the click event. This can be achieved by adding EventListener method. Now whenever the Search button will be clicked a set of operation would perform on that Keyword. 




document.querySelector(".js-go").addEventListener("click", function () {
  var inputValue = document
    .querySelector(".js-userinput").value;
  var userInput = getUserInput();
  searchGiphy(userInput);
});

Note: There are other Events in the addEventListener method through which we can extend the ways of listening the events. Here “keyup” event is used to allow the user to press Enter and search the keyword for the images. In contrast, it actually listens which key user has pressed. Here an object is required to check whether the key pressed is matched to the KeyCode Value or not. 




document.querySelector(".js-userinput")
  .addEventListener("keyup", function (e) {
  
  // If the Key Enter is Pressed 
  if (e.which === 13) { 
    var userInput = getUserInput();
    searchGiphy(userInput);
  }
});

Step-3: Once the Input data is captured by the Event Listener it is now used by the searchGiphy function which takes the user data as a parameter and performs a set of operation.

searchGiphy( searchQuery ): The function handle the following things-

Loading the Images without Refreshing the Page




function searchGiphy(searchQuery) {
  var url =
  + searchQuery;
      
  // AJAX Request
    
  var GiphyAJAXCall = new XMLHttpRequest();
  GiphyAJAXCall.open("GET", url);
  GiphyAJAXCall.send();
  
  GiphyAJAXCall.addEventListener("load", function (data) {
    var actualData = data.target.response;
    pushToDOM(actualData);
    console.log(actualData);
      
  });
}

Step-4: A function pushToDOM is defined which will be responsible for processing the response and loading the Images.
pushToDOM(response): There are some standard steps to handle the responses from the API. This function handles the following things-

Thus, we can process all the images in the responses by looping over the images which extract each image into a container with its source url.




function pushToDOM(response) {
  
  // Turn response into real JavaScript object
  response = JSON.parse(response);
    
  // Drill down to the data array
  var images = response.data;
  
  // Find the container to hold the response in DOM
  var container = document.querySelector(".js-container");
    
  // Clear the old content since this function 
  // will be used on every search that we want
  // to reset the div
  container.innerHTML = "";
  
  // Loop through data array and add IMG html
  images.forEach(function (image) {
  
    // Find image src
    var src = image.images.fixed_height.url;
  
    // Concatenate a new IMG tag
    container.innerHTML += "<img src='" 
      + src + "' class='container-image' />";
  });
}

Final Output:

Source Code: https://github.com/codewithdev/Gif-Search-Engine 


Article Tags :