0

How can I execute the link I have stored in var Link ? I have tried the eval() but that doesn't seem to do much.

I have stored the link as a string just like shown.

var link = "someURLlink.com";

This is placed inside a function so when I call the function, I want this link to be executed and then I am going to parse the result.

Edited: This will make a call to an API for authorization with the client_id hard coded into the URL. After I receive the secret code from the landing page, I am going to redirect back to my original HTML document.

OjenG
  • 31
  • 1
  • 6

2 Answers2

2

Using something like jQuery this is a very easy task. You can do it without jQuery, but it is a bit harder, so start at jQuery, and factor it out if you want to.

http://api.jquery.com/jquery.get/

$.get(link, function( data ) {
  alert( "Link was loaded. I could mess with data which will be the response." );
});
Tim
  • 2,878
  • 1
  • 14
  • 19
  • Are people who downvote too lazy to say why? This is a perfect answer, and yet I get downvoted. What a toxic environment for people to answer in. – Tim Mar 29 '15 at 18:24
  • 1
    It is generally not ok to answer "use jQuery" when the question doesn't specifically say that using jQuery is ok. And this answer is missing crucial information about cross-origin requests. This will probably not work for the OP because of that. – JJJ Mar 29 '15 at 18:25
  • Where does it say it is not ok to suggest frameworks? This isn't assemblyOverflow.com, we use tools. Some of those tools make things like AJAX requests infinitely simpler for people that are very clearly very junior developers. One such tool is jQuery. And since he didn't say he cared whether I suggested tools, I gave him a suggestion for what I thought was most likely to help him succeed. As for cross-origin, I agree it may bite him. But without knowing I have to assume CORS/similar is on, and try to help. – Tim Mar 29 '15 at 18:31
  • http://meta.stackexchange.com/questions/45176/when-is-use-jquery-not-a-valid-answer-to-a-javascript-question – JJJ Mar 29 '15 at 18:32
  • I didn't just respond "Use jquery" like the posted example. I gave a specific API within jQuery that would help him solve his problem. And to quote the second answer, When any of the following is true: It is the entire answer, with no explanation of exactly how jQuery helps When the problem can be solved simply without jQuery When the question specifically asks for no javascript frameworks When the question is tagged or mentions a competing framework When someone else already posted it – Tim Mar 29 '15 at 18:35
  • @Tim, I appreciate the lead. You are right, I asked it open ended to see my possibilities. I would upvote but I am new to SOF. Need to gain more reputations. – OjenG Mar 29 '15 at 19:28
2

The answer using jquery is valid but if you want to do it purely in your own code use the xmlhttprequest object

http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",link,false);
xmlhttp.send();
var parseMe = xmlhttp.responseText;
vbranden
  • 5,814
  • 2
  • 22
  • 15