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?
-
Yes, it is doable. You can use Ajax.Racil Hilan– Racil Hilan2018-01-28 08:36:53 +00:00Commented 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.Mario Nikolaus– Mario Nikolaus2018-01-28 08:37:09 +00:00Commented 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.Racil Hilan– Racil Hilan2018-01-28 08:41:33 +00:00Commented 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?Nadim– Nadim2018-01-28 08:52:31 +00:00Commented 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.Racil Hilan– Racil Hilan2018-01-28 08:57:54 +00:00Commented Jan 28, 2018 at 8:57
Add a comment
|
1 Answer
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.