2

I would like to know if it is possible in JavaScript to access to a given URL without opening its web page in a browser . Actually, what I'm really trying to do is parsing through a page (given its URL) and clicking on the first link that it contains without even opening that web page in my browser. Is that doable with JavaScript. In case it is, how should I do that? What function (or functions) should I use? In case it is not, what would the alternative solutions be?

5
  • Yes, it is doable. You can use Ajax. Commented Jan 28, 2018 at 8:36
  • I guess you don't know how http works? or Internet for that matter... You cannot access a resource if you don't request for it, it is that simple... From your question it looks like you either didn't express yourself in a good way or you simply don't understand some concepts. Commented Jan 28, 2018 at 8:37
  • @MarioNikolaus Yes, the wording is poor, but if you read the description, it is pretty clear. Not everybody's first language is English, so as long as the description is understandable, we can be flexible with the poor wording. He doesn't want to visit the URL, he wants to make an HTTP request and process the result. Commented Jan 28, 2018 at 8:41
  • @RacilHilan: Indeed! That's what I wanted to say. What function should I use in order to process an HTTP request result so that I can make a click on a link (that exists in that page) automatically? Commented Jan 28, 2018 at 8:52
  • That is a too broad question. See my answer for examples to get you started. I provided links, so you can read and try to implement a solution. If you face some issues, come back, post your code in a new question, and we'll be glad to help you. Commented Jan 28, 2018 at 8:57

1 Answer 1

2

What you need is to make an HTTP request to the URL and process the results. You can do that in JavaScript using the XMLHttpRequest object. Example:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    console.log(this.responseText);
  }
};
xhttp.open("GET", "put_the_URL_here", true);
xhttp.send();

However, it is easier to use a library like jQuery.Ajax for that:

$.ajax({
  url: "put_the_URL_here",
  context: document.body
}).success(function(data) {
  console.log(data);
});

For this to work, the URL that you're trying to access must have CORS enabled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.