zoukankan      html  css  js  c++  java
  • JS远程获取网页源代码的例子

    js代码获取网页源代码。

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

    <html> 
    <head> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
    <title>远程网页源代码读取-脚本学堂-www.jbxue.com</title> 
    <style type="text/css"> 
    /* 页面字体样式 */ 
    body, td, input, textarea { 
    font-family:Arial; 
    font-size:12px; 

    </style> 
    <script type="text/javascript"> 
    //用于创建XMLHttpRequest对象 
    function createXmlHttp() { 
    //根据window.XMLHttpRequest对象是否存在使用不同的创建方式 
    if (window.XMLHttpRequest) { 
    xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式 
    else { 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式 


    //直接通过XMLHttpRequest对象获取远程网页源代码 
    function getSource() { 
    var url = document.getElementById("url").value; //获取目标地址信息 
    //
    地址为空时提示用户输入 
    if (url == "") { 
    alert("请输入网页地址。"); 
    return

    document.getElementById("source").value = "正在加载……"; //提示正在加载 
    createXmlHttp(); //创建XMLHttpRequest对象 
    xmlHttp.onreadystatechange = writeSource; //设置回调函数 
    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 

    //将远程网页源代码写入页面文字区域 
    function writeSource() { 
    if (xmlHttp.readyState == 4) { 
    document.getElementById("source").value = xmlHttp.responseText; 


    </script> 
    </head> 
    <body> 
    <h1>远程网页源代码读取</h1> 
    <div> 
    地址:<input type="text" id="url"> 
    <input type="button" onclick="getSource()" value="获取源码"> 
    </div> 
    <textarea rows="10" cols="80" id="source"></textarea> 
    </body> 
    </html> 
  • 相关阅读:
    5.MFC基础(五)视图、运行时类信息、动态创建
    4.MFC基础(四)菜单、工具栏、状态栏
    OpenCV Python 4.0安装
    windows批量导出文件名到txt
    *&p理解
    VS调试快捷键配置更改
    数组类的创建(下)
    数组类的创建(上)
    operator用法:隐式类型转换
    C++单例模式
  • 原文地址:https://www.cnblogs.com/linuxnotes/p/3304710.html
Copyright © 2011-2022 走看看