4

Nothing gets printed on the web page when I try to open an HTML file having javascript. The script inside the html code loads the xml file and tries to print some element data. I have pasted the code below. Sadly no data of file gets printed. I have used browsers IE8 and Chrome. Please let me know what is the issue.

<!DOCTYPE html>
<html>
    <head>
        <script>
            function loadXMLDoc(dname)
            {
                if (window.XMLHttpRequest)
                {
                    xhttp=new XMLHttpRequest();
                }
                else
                {
                    xhttp=new ActiveXObject("Microsoft.XMLDOM");
                }

                xhttp.open("GET",dname,false);
                xhttp.send();
                return xhttp.responseXML;
            }
        </script>
    </head>
    <body>
        <script>
            xmlDoc=loadXMLDoc("file:///E:/Parser/book.xml");

            document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue + "   <br>");
            document.write(xmlDoc.getElementsByTagName("authors")[0].childNodes[0].nodeValue + "<br>");

        </script>
    </body>
</html>
2
  • 1
    You're definitely not going to get any joy with Chrome since you're using ActiveX. Any chance you can build a web service to surface that XML file? Commented Aug 17, 2013 at 20:24
  • any errors in your console? Commented Aug 17, 2013 at 20:25

1 Answer 1

4

You can't open a local file using ajax

xmlDoc=loadXMLDoc("file:///E:/Parser/book.xml"); 

this can't be performed because JavaScript running in a web browser does not have access to the local filesystem. That would a huge security risk.

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

6 Comments

It does have access to the local file system. The problem is that remote viewers don't have access to his file system.
Please suggest me the alternatives or a possible solution to this problem. I have got this code from W3C website. Thanks
@user2281107 you can point a web URL for an XML file. And to make it work, you can use an npm package named http-server.
The only way that javascript running on a web browser allows access to a file system, is through an <input type="file"> to let the user be aware of that specific action. When done programmatically, it can't access the file system.
|

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.