+ 5
Javascript Html Request
How to get source code of a website or url entered. Is there any method for this? Please let me know for this.
5 Respostas
+ 1
You can use jQuery to make an AJAX request that gets the html from an external url.
It would look something like this:
$.ajax({
url: "external url here"
}).done(html => {
// Do stuff with html here.
});
AJAX Information: http://api.jquery.com/jquery.ajax/
+ 1
Brian R can u please add code reference
+ 1
If you are talking about html code, simply press Ctrl+U and you can see them. But for server-side code you need to hack the server in some ways to download the script file instead of being executed (Do not ASK HOW, uncle Google knows all).
If you want to do it programatically (grab HTML source code) here's how
Using JS (vanilla precisely)
function ajaxRequest(reqUrl) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//Do something with the code(response, in fact)
//Your response lies in this variable this.responseText;
console.log(this.responseText);
}
else
{
// Handle error here, if something goes left
}
};
xhttp.open("POST", reqUrl, true);
xhttp.send();
}
//Calling it
ajaxRequest('your/url/of/preference.ext'); //.php, html or whatever
//Beware of *same origin policy*
0
You can't get source code of a website if they are loading dynamically pages. Some websites are made like that.
You can get source code even full website using a software which is available on Internet. But it converts full website to html source code.
0
You will only get html code if you will open url and left click and click on inspect