Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reddit fetch api js

var menuEl = document.querySelector('#menu');
var entriesEl = document.querySelector('#entries');

function fetchSubreddit(url) {
  if (url) {
    fetch('https://www.reddit.com/r/' + url + '.json').then(function(response) {
      return response.json();
    }).then(function(json) {
      var links = '';
      for (var i = 0; i < json.data.children.length; i++) {
        links += '<li><a href="' + json.data.children[i].data.url + '">' +
          json.data.children[i].data.url + '</a></li>';
      }
      entriesEl.innerHTML = '<ul>' + links + '</ul>';
    });
  }
}

var subredditsByTopicUrl = 'https://www.reddit.com/api/subreddits_by_topic.json?query=javascript';
fetch(subredditsByTopicUrl).then(function(response) {
  return response.json();
}).then(function(json) {
  var select = document.createElement('select');
  var links = '';
  for (var k = 0; k < json.length; k++) {
    links += '<option value="' + json[k].name + '">' + json[k].name +
      '</option>';
  }
  select.innerHTML = links;
  select.addEventListener('change', function(e) {
    fetchSubreddit(e.target.value);
  });
  menuEl.appendChild(select);
}).catch(function(ex) {
  ChromeSamples.log('Parsing failed:', ex);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Activelink.js 
Javascript :: Centos install update downgrade nodejs 
Javascript :: Split string into words, without punctuation 
Javascript :: if array ontains any item of another array js 
Javascript :: js reverse a strings in array 
Javascript :: canvas drawimage resize quality 
Javascript :: accepting form data node js 
Javascript :: MONGOOSE update on by body 
Javascript :: redux store 
Javascript :: scroll bar disappears after closing modal 
Javascript :: socket.id 
Javascript :: handling event in jsx 
Javascript :: how to copy an arry react 
Javascript :: null value check in react js 
Javascript :: what is a promise 
Javascript :: jwt strategy 
Javascript :: js return the highest and lowest number 
Javascript :: Upload a file using ExpressJS+Multer 
Javascript :: js get datatable attr value on click 
Javascript :: Using axios send a GET request to the address: 
Javascript :: JavaScript throw with try...catch 
Javascript :: javascript function return boolean 
Javascript :: how to concatenate a string in javascript 
Javascript :: how to swap two images in javascript 
Javascript :: update in mongoose node js 
Javascript :: js event target 
Javascript :: overflowx javascript 
Javascript :: js add element to array 
Javascript :: js jwt decode 
Javascript :: js check data type 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =