0
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.

3
  • what response do you expect...? Commented Nov 7, 2013 at 5:21
  • 2
    The XMLHTTPRequest object does not care if the URL to fetch ends with a .txt or an .html What is likely happening is that the ajax.responseText is invalid HTML which causes a problem when you try to add it to your document. Add a printout of ajax.responseText to verify Commented Nov 7, 2013 at 5:22
  • Hot f12 in ie or chrome and look in the console. HTML should be well formed XML Commented Nov 7, 2013 at 5:23

2 Answers 2

4

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.