I finally figured it out. I basically had to
- Disable the framework/script used for handling the hash changes
- Change the URL to point to the main content
- Restore the original URL and re-enable what I disabled in #1.
For example, I had an event like this:
var onPageChange = function (newLocation, historyData) {
// load page
};
First, I changed it to:
var pageChangeEnabled = true;
var onPageChange = function (newLocation, historyData) {
if (pageChangeEnabled) {
// load page
}
};
And then, the Skip Navigation
link looks like this:
<a href="javascript:pageChangeEnabled=false;var oh=location.hash;location.hash='#content';location.hash=oh;pageChangeEnabled=true;void(0);" id='skipNav'>Skip Navigation</a>
For easier understanding, this is the code in the href
:
// disable navigation
pageChangeEnabled=false;
// save current hash
var oldHash=location.hash;
// update the hash/anchor, make everything work
location.hash = '#content';
// restore the old hash, as there are no elements that have the same
// name of the URL, it works
location.hash = oldHash;
// re-enable page navigation
pageChangeEnabled=true;
// void(0); is just to avoid the navigation of the link
void(0);
UPDATE: As explained by Adam, this does not fully work as keyboard navigation does not change. I ended up doing: $('#content').attr('tabindex', '-1').focus();