JS gets the source code of the web page
This article introduces the relevant knowledge of "JS obtaining web page source code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
JS gets the source code of the web page
Remote web page source code read / * page font style * / body, td, input, textarea {font-family:Arial; font-size:12px;} / / used to create XMLHttpRequest object function createXmlHttp () {/ / use different creation methods if (window.XMLHttpRequest) {xmlHttp = new XMLHttpRequest () depending on whether the window.XMLHttpRequest object exists / / creation methods supported by browsers such as FireFox and Opera} else {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); / / creation methods supported by IE browsers}} / / obtain remote web page source code function getSource () {var url = document.getElementById ("url") directly through XMLHttpRequest object. Value / / get destination address information / / prompt the user to enter if (url = "") {alert ("Please enter a web address if the address is empty.") ; return;} document.getElementById ("source"). Value = "loading." / / prompt that createXmlHttp () is being loaded; / / create XMLHttpRequest object xmlHttp.onreadystatechange = writeSource; / / set callback function xmlHttp.open ("GET", url, true); xmlHttp.send (null) } / / write the remote web page source code to the page text area function writeSource () {if (xmlHttp.readyState = = 4) {document.getElementById ("source"). Value = xmlHttp.responseText;}} remote web page source code read address: