Thursday, September 8, 2011

epub: Hyperlinks in ePub Books

I also have a site at www.jedisaber.com/ebooks where I talk about how to make ePub files by hand. I'm going to do some blog posts here with bits and pieces about the ePub format that don't fit in the guide. I thought this might be of interest, since I've almost decided to self-publish.

Anyway, today's topic, links in ePub files.

I've gotten a few questions about how to do links inside of ePub books. This question is both easy to answer, and complicated.

The easy answer is: Just like you would in a normal web-page. This is complicated by the fact that the ePub specification does not give any usage guidelines on how to handle links.

iBooks, Stanza, and Adobe Digital Editions all handle it this way:

If you have an eBook with two files in it, say, "chap01.xhtml" and "chap02.xhtml", and they are in the same sub-folder in the zip file, adding a link is easy:

<a href="chap02.xhtml">Link title here</a>

Put that code somewhere in the chap01.xhtml file, and it'll jump you right to it. This is handy if you want to include a Table of Contents, or link to annotations or something similar. (Yes, if you use the TOC.ncx file, the ePub book will have a Table of Contents, but only through the menu. If you want one to show up as part of the book when you're paging through it, you have to make a separate page with links.)

Also, note that all links in ePub files are relative links. If your folder structure looks like this:

Mimetype
OEBPS
  - title.xhtml
  - chap01.xhtml
Extras
  - annotations.xhtml

You can link between title and chap01 by just using the filename, not the full path. If you want to link from chap01 to annotations.xhtml, your link would have to look like this:

<a href="../extras/annotations.xhtml">link text</a>

(Please note that while you can put files in different folders like this, it's not recommended.)

Linking outside of the book can be interesting. The standard link:

<a href="http://www.jedisaber.com/">Link</a>

works in iBooks, Stanza, and Digital Editions. I'm not sure if it works in others, due to limited testing ability. (I don't have a Sony Reader or Nook.)