Please help with random quote machine project

Please help with random quote machine project
0.0 0

#1

Hi all. Attempting to make it through the quote machine challenge. Got through everything but the tweet option. I am trying to go through this all using vanilla javascript, no jquery so most of the examples I am finding are jquery and not helping. I am currently coding everything in atom not code pen so ill copy in my javascript code below. Please help, Thank you!

document.getElementById(“btn”).addEventListener(“click”, loadData);
function loadData(){

var xhr = new XMLHttpRequest();

xhr.onload = function (){

  var data = JSON.parse(xhr.responseText);
  document.getElementById("one").innerHTML = data.quote;
  document.getElementById("two").innerHTML = "- " + data.author;
  document.getElementById("hide").style.display = "none";
} 
xhr.open("GET","https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous&count=10", true);
xhr.setRequestHeader("X-Mashape-Authorization", "h2fJssMbfjmsh6JCwB1I5lWmwbc0p1XV8SUjsnHRlasBuAN8pg");
xhr.send();
document.getElementById("twitter").addEventListener("click",tweet);

function tweet (){
document.getElementById(“twitter”).href = “https://twitter.com/intent/tweet?text=” + data.quote + data.author;
}
};


#2

Not sure specifically what you require assistance with, but it looks like your data object is defined locally inside your xhr.onload function. If you define it outside the function, then you can re-use it in your tweet() function.

Also not sure if it’s necessary, but the spaces in the string of text you send to twitter may need to be replaced with %20. Again, not sure on this, so I wouldnt think about it at all unless you get some weird stuff happening with your twitter function.


#3

Sorry! I didnt specify that I am having issues sending my quote to twitter. It wont open th twitter window any longer and when it didnt it wouldnt load my qoute in as the text.


#4

Mh…how the html part is structured? I mean, what the ‘tweet’ function does is to change the link ( of an anchor) to the one you provide, but can you click the link then? I mean, it’s not necessarily in your js the issue ^^

P.S:
Most users use window.open if i remember correctly, which is not a JQuery object :slight_smile: