Search code examples
javascriptfileepub3

How to read the contents of a file inside an epub using javascript


I need to read, inside a page of an epub3 book, the contents of one of the file of that same epub, being some data to process with a javascript function. Unfortunately, Javascript prevents from loading local files for security reasons. (e.g the File API only allows loading uploaded user files).

But for me in the context of an epub3, it makes sense and I didn't find any information in the IDPF EPUB3 documentation related to this topic.

Any idea ?

OK. Let's clarify:

I have an epub3 with the following structure:

<root>
    META-INF
        ...
    OEBPS
        pages
            page.xhtml
        data
            data.xml

In page.xhtml, I want to write in Javascript something like:

<script type="text/javascript">
//pseudo code
var indata = readAsText("../data/data.xml"); // how to write that ???
var outdata = myfunction(indata);
</script>

Solution

  • Found the solution for ages, and realized that it had never been answered: So answering to my own question ;-)

    <script type="text/javascript">
        /* Load the file using HTTP GET */
        $.get( "../data/data.xml", function( indata ) {
        var outdata = myfunction(indata);
        }, 'text');
    </script>