JS remote access to Web page source code example _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Remote Web page source code read </title>
<style type= "Text/css" >
/* Page Font style * *
Body, TD, input, TEXTAREA {
font-family:arial;
font-size:12px;
}
</style>
<script type= "Text/javascript" >
Used to create a XMLHttpRequest object
function Createxmlhttp () {
According to window. Whether the XMLHttpRequest object exists using a different creation method
if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest (); Firefox, Opera and other browsers support the creation of the way
} else {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");//ie browser-supported creation
}
}
Get the Remote Web page source code directly from the XMLHttpRequest object
function GetSource () {
var url = document.getElementById ("url"). Value; Get Destination Address information
Prompt user for input when address is empty
if (url = = "") {
Alert ("Please enter a Web address.") ");
Return
}
document.getElementById ("source"). Value = "Loading ..."; Prompt is loading
Createxmlhttp (); Creating XMLHttpRequest Objects
Xmlhttp.onreadystatechange = WriteSource; Set callback function
Xmlhttp.open ("Get", url, True);
Xmlhttp.send (NULL);
}
Write Remote Web page source code to page text area
function WriteSource () {
if (xmlhttp.readystate = = 4) {
document.getElementById ("source"). Value = Xmlhttp.responsetext;
}
}
</script>
<body>
<div>
Address: <input type= "text" id= "url" >
<input type= "button" onclick= "GetSource ()" value= "Get Source" >
</div>
<textarea rows= "cols=" id= "source" ></textarea>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.