Loading web page in a div using Ajax

Using Iframe, it is very easy to load web page in your page. But if you want to load page in a div, Ajax can be helpful.

    <script type="text/javascript">
    function makeRequest(url) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up 😦 Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { state_Change(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send(null);

    }

    function state_Change(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                var s = httpRequest.responseText;
                s = s.slice(s.indexOf("<fo" + "rm"));
                s = s.replace("</body>","");
                s = s.replace("</html>","");
                s = s.replace(/<form/ig,"<formdisabled");
                alert("nnn");
                document.getElementById("ext").innerHTML=s;
            } else {
                alert("Problem retrieving data:" + httpRequest.statusText);
            }
        }
    }
    </script>

 

In the design mode,
<body>
    <form id="form1" runat="server">
    <div>
        <div id="ext">
        </div>
        <input type="button" onclick="makeRequest('http://amar-desh.net/');" />
    </div>
    </form>
</body>
Advertisements

One thought on “Loading web page in a div using Ajax

  1. Hello ,Please can u tell me or provide the code that how can i load a page in div using Ajax in asp.net

    Waiting for your reply,

    Thank you

    Regards
    Amit Habu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s