0

In the <head> section of my HTML document, I have linked an XML file:

<link id="data" type="text/xml" href="Data.xml" />

I would like to read the contents of this file via JavaScript, but I can't even seem to get access to the contents of the file.

I tested it this way:

<script type="text/javascript">
    var link = document.getElementById("data");
    document.write(link.innerText);
    document.close();
</script>

What should I try next?

P.S. ...Can't use AJAX for this since it's all local files.

1
  • If it's all local files, and you're on Windows, then you could create an HTA and load the XML via MSXML. Commented Jul 8, 2015 at 23:04

1 Answer 1

1

The link doesn't have a rel attribute, so the browser doesn't do anything with the URL (not that any value for rel means "Download and make it available to JS").

It doesn't have any innerText because it is an element defined as being empty. It exists only for the attributes it has on it.

You'll need to get the URL by using getAttribute and then fetch the data using an XMLHttpRequest object (i.e. Ajax).

The usual security restrictions for local files will apply. So you'll need to use a browser that supports access to them via XMLHttpRequest or install a local web server.

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

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.