RSS

Category Archives: Crwalling

Grab webpage contents using javascript

You can read/fetch web page contents using  javascript.  The page should be available on the live URL. 

Please find the code below:

<script type=”text/javascript”>
    var PageNm;
    var XmlHttpObj;
    var requestURL = ‘getServerTime.aspx’;
    var is_ie = (navigator.userAgent.indexOf(‘MSIE’) >= 0) ? 1 : 0;
    var is_ie5 = (navigator.appVersion.indexOf(“MSIE 5.5”) != -1) ? 1 : 0;
    var is_opera = ((navigator.userAgent.indexOf(“Opera6”) != -1) || (navigator.userAgent.indexOf(“Opera/6”) != -1)) ? 1 : 0;
    //netscape, safari, mozilla behave the same???
    var is_netscape = (navigator.userAgent.indexOf(‘Netscape’) >= 0) ? 1 : 0;  

 

    function GetResponse() {

        var xhr = CreateXmlHttpObj();
        // Page URL needs to be written here
        xhr.open(“GET”, “Web Page URL”, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    statusDiv = document.getElementById(“stats”);

                    alert(xhr.responseText);
                } else {
                    alert(‘error!’);
                }
            }
        };
        xhr.send(null);
    }

    function CreateXmlHttpObj() {
        // try creating for IE (note: we don’t know the user’s browser type here, just attempting IE first.)
        try {
            if (is_ie) {
                //The object to create depends on version of IE
                //If it isn’t ie5, then default to the Msxml2.XMLHTTP object
                var strObjName = (is_ie5) ? ‘Microsoft.XMLHTTP’ : ‘Msxml2.XMLHTTP’;

                //Attempt to create the object
                try {

                    XmlHttpObj = new ActiveXObject(strObjName);
                    //XmlHttpObj.onreadystatechange = handler;
                }
                catch (e) {
                    //Object creation errored
                    alert(‘IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled’);
                    return;
                }
            }
            else if (is_opera) {
                //Opera has some issues with xmlHttp object functionality
                alert(‘Opera detected. The page may not behave as expected.’);
                return;
            }
            else {
                // Mozilla | Netscape | Safari
                XmlHttpObj = new XMLHttpRequest();
               
            }

 
        }
        catch (e) {
            try {
                XmlHttpObj = new ActiveXObject(“Microsoft.XMLHTTP”);
            }
            catch (oc) {
                XmlHttpObj = null;
            }
        }
        // if unable to create using IE specific code then try creating for Mozilla (FireFox)
        if (!XmlHttpObj && typeof XMLHttpRequest != “undefined”) {
            XmlHttpObj = new XMLHttpRequest();
        }

        return XmlHttpObj;
    }

</script>

 

 
1 Comment

Posted by on September 3, 2009 in Crwalling

 

Tags: , , , ,