Links to anchor id/tags are ignored
While building a table of contents I've been unable to get links to id sections/anchors to work. In all my tries the link simply takes me to the top of the target page.
Basically my link is <a href="03.html#sec2">, and for the anchor I've tried both the HTML5 way <h2 id="sec2">Title</h2> and the old way <a id="sec2" name="sec2">. They both work in Safari but seem to be ignored in Baker.
This is indeed a bug, we'll check! ;)
Added support for links destination anchor. Closed by d9f31ea4909473c3f84e7c0d126b49ec2ca6e133
The new code sorts it out nicely. The only thing I had to change was "document.getElementsByName('%@')[0].offsetTop" with "document.getElementById('%@').offsetTop" in handleAnchor so it works with id attributes (I'd already built my table of contents based on ids). I guess both id and name anchors should be handled.
Thank you very much!
Thank you aldriq for pointing this out! I'll commit an improved method as soon as possible :)
I've commited the improved method for handling anchors, now it works for both id and name attributes :)
Be aware that there seems to be an issue with UIWebView: click on link seems to pass from "shouldStartLoadWithRequest" only once (ref: http://stackoverflow.com/questions/1840355/uiwebview-shouldstartloadwithrequest-only-called-once). Click on another link or reload the page reset this strange behavior.
So, while in Baker we handle hash reference to both id and name attributes now (as W3C standard), the second click on a link is handled by the UIWebView itself, which doesn't seems to handle anchor to name attributes (only outside Safari Mobile... there it all works perfectly).
I'll look more into this issue, for now I suggest to point hash reference to tag with the id attribute set.
Thank you again aldriq, your suggestion has proved really useful
Thanks for all the thorough research, I never thought this would open such a little can of worms...
Can we get a quick example how to use anchors in Baker?
Right now I'm using <a name="zoom01" id="zoom01" href="zoom/zoom-001.html"> to link to a page and <a href="../001.html#zoom01"> to go back. It's not working in Baker (it does in Safari), I guess I misread something :)
Hi!
A quick example of an anchor ... let's see ...
• put a link link to page 5 in your page 001.html. • in the page 005.html assign the id="my-anchor-1" to an element. • click on the link on page 001.html and the book should go to page 005.html and then scroll down to you element id="my-anchor-1"
That's it, nothing more :)
Thanks, I'll try this later if I still need it :)
We did some additional tests:
- Links from a page of the book to a page of the book with anchor, goes there correctly.
- Links from a page inside an iframe to a page of the book with anchor, goes there correctly.
- Links from a page of the book to a page outside the book with an anchor, goes there correctly.
- Links from a page outside of the book (opened like point 3 above for example) to the page previously there, goes there correctly.
- Links from a page outside of the book (opened like point 3 above for example) to a different page inside the book, goes there correctly.
We have enabled the items 3, 4, 5 above with the recent commit to try address the issues you noted. :)
I think that should cover all the basic anchor situations. :)
Hi - I'm attempting to do something like enable your item "1" in your list of 5 items above:
- Links from a page of the book to a page of the book with anchor, goes there correctly.
Without this, I am not able to have the page and book behavior I'm looking for, which is:
a) Tap an anchor link from a TOC page to go to a horizontal anchor in the destination page.
b) Have this destination page reside in the book.json manifest.
I am able to of course get the horizontal anchor to work in a page not included in the book.json list, since this forces the book app to open this page in a UIWebView, which obeys horizontal anchor links. However, what this means is that this new page basically replaces my TOC page, and thus will swipe left and right to the pages consecutively before / after the TOC page - not its own pages since it doesn't exist in the book.json chronology.
It would be great to be able to horizontally anchor on a page and still have this page swipe left or right to its own consecutive pages.
I tried to create a goRightInPage pragma mark, and use this to modify the handleAnchor mark, but it didn't work.
Any suggestions?