user2836879
user2836879

Reputation: 1

Is it possible to load from a .HTML file with Ajax?

function loadAjax(filename){
    var ajax;
    if(window.XMLHttpRequest){
        ajax=new XMLHttpRequest();
    }
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4 && ajax.status==200){
            document.getElementById("target").innerHTML = ajax.responseText;
        }
    }
    ajax.open("GET",filename,true);
    ajax.send();
}

If filename is a .txt file, this works as expected. However, if it is a .html file, no response is shown.

Upvotes: 0

Views: 77

Answers (2)

Vidya
Vidya

Reputation: 30310

The answer is of course yes, but I would suggest using JQuery's load function as described here. It is just so much easier to use an abstraction rather than mess with the low-level details of the XHR and the DOM.

This assumes your HTML is valid.

In your case, the code would be something like $("#target").load(filename);

Upvotes: 4

Sridhar R
Sridhar R

Reputation: 20418

Yes its Possible Use JQuery load

Script

<script type="text/javascript">
   $(document).ready(function(){
    $("#urButton").click(function() {
    $("#urDiv").load("trackingCode.html");
    });
   });
</script>

HTML

<button id="urButton">Click Me</button>
<div id="urDiv"></div>

Upvotes: 1

Related Questions