3

I am working on a simple project which involves loading local .xml file into DOM structure by local .html file. We can assume that .html and .xml file are placed in the same folder on the same computer. Problem is that IE 11 disallows any interaction with local xml file. (SCRIPT5: Access is denied.)

So far i tried this solutions (Solution 1,2 are tested and functional within Mozilla FireFox and Google Chrome, Microsoft Edge has some different problem - see first code snippet):

Synchronous/Asynchronous XMLHttpRequest (async in example)

function loadXMLDoc(doc)
{
    try{
        xmlhttp = new XMLHttpRequest();
    }catch(e){
        try {
            xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch(e){
            try {
                xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
            }
            catch(e)    {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)    {
                    alert("XMLHTTP Not Supported On Your Browser");

                }
            }
        }
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) /*Microsoft edge returns status 0 here */
        {
            alert(xmlhttp.responseText);
        }
    };
    xmlhttp.open("GET",doc,true);/*IE11 prints "SCRIPT5: Access is denied." into console*/
    xmlhttp.send();
}

And JQuery async solution

window.onload = function() {
$.ajax({
        url: "output.xml",
        aync: true,
        success: myHandle,
        isLocal:true,
        dataType: "xml"
    });
}

function myHandle(data) {
    alert(data);
}

Third solution consists of simple node.js webserver (see Using node.js as a simple web server) but this seems to be a too large gun for me. Also there is a problem that web server has to be start explicitly via cmd / script, but i just want to hit .html and see interpreted xml data.

TL;DR My questions are:

  • Is there any workaround that makes local .xml files accessible for IE11?
  • Why is this "security risk" for IE but not for others?

Note: Since .xml file can have more Mb, async solutions are prefered for me.

Thank you.

Community
  • 1
  • 1
Smarty77
  • 1,208
  • 3
  • 15
  • 30
  • is your end-goal to just render XML in a specific way that it looks like a page? Is your XML static or it is generated, in case when it is generated do you have a possibility to affect the generating process? – smnbbrv Aug 25 '15 at 10:11
  • Xml is generated (according to certain DTD). Anyway your expectations are right i just need to render local page modules (i.e. graphs/tables), where data are loaded from xml. – Smarty77 Aug 25 '15 at 10:14
  • do you stick to JS solution? Or only results count for you? – smnbbrv Aug 25 '15 at 10:16
  • Results and performance count for me. But it needs to run after opening "index.html" inside same folder as "file.xml". – Smarty77 Aug 25 '15 at 10:19
  • Did you try using a URL instead of a local path? – dr_dev Aug 25 '15 at 10:24
  • If i change just "output.xml" string onto : "file:///D:/repositories/.../output.xml", it does not work. Chrome reports this: "XMLHttpRequest cannot load file:///D:/repositories/.../output.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource." – Smarty77 Aug 25 '15 at 10:33
  • 1
    Well, mentioning it as "output.xml" or "file://D:/...." are both same, and refer to a local path. What I meant is, if you have a web server, you can put the file there, and provide the path as "http:///localhost/..../output.xml", and see if it helps. – dr_dev Aug 25 '15 at 10:39
  • Wow, thanks @dr_dev , yes path "http://localhost:63342/.../output.xml" really works! – Smarty77 Aug 25 '15 at 10:46
  • You're welcome. Posted it as answer. – dr_dev Aug 25 '15 at 10:53

2 Answers2

1

If you're trying to provide the file path as a local path, then it won't work in other browsers as well, using plain javascript, and you may get Cross domain errors. If you have a web server, you can put the file there in the appropriate location, and provide the path as "http://localhost/.../file.xml". This may help.

dr_dev
  • 492
  • 3
  • 17
  • Still, there is one question unanswered. I assume that in localhost:63342/.../output.xml". 63342 is port number. I suppose it can change? And if so is there a generic solution? – Smarty77 Aug 25 '15 at 10:59
  • Yes, the number appearing after localhost is the port no. If the port no. changes the URL has to be changed as well. – dr_dev Aug 25 '15 at 11:02
  • yes but is there a way to find the port no inside js? location.port doesnt work if you open html locally (i.e. it starts with your local path in browser) – Smarty77 Aug 25 '15 at 11:17
  • No, there is no way you can find the port number from within javascript. – dr_dev Aug 25 '15 at 12:07
0

Well, what I propose as a backup opportunity is to go away a bit from the html + js solution and try XML + XSLT. This should not have any security issues, the only thing that changes for you - you don't need to open index.html but you'll need to open output.xml in your browser.

Also, you would need to add to your XML file the pointer to your XSLT file, see e.g. here how to do that.

Once you loaded your XML and processed it with XSLT you have the same HTML with the same JavaScript, but all data is already rendered. XSLT is quite powerful and I am sure it will fulfill all your requirements

Community
  • 1
  • 1
smnbbrv
  • 23,502
  • 9
  • 78
  • 109