Go to page#

  1. 5 years ago

    How do I jump to a specific page# using Javascript? goToNextPage() works but I want goToPage(n) and it doesn't.
    In addition, is there a "pool" of Javascript examples for PubCoder?
    Thanks,
    Ranco

  2. Giancarlo N

    28 Dec 2015 Administrator

    I suggest you to use the specific function to achieve this, i.e. "Go To Page". It lets you select the target page to go to. Using javascript would be very tricky because you should have to manual insert the correct url that depends on the structure of your project. Anyway if you really need to use javascript consider correct url is '../<pagenum>/page.xhtml' where pagenum is the number of page to go to, but I highly recommend you to use the prebuild function "Go To Page"

  3. My reason to do it via Javascript is that I want to jump to a random page when the user hits a certain button. Is there a way to do it somehow using the built-in function, maybe combined with Javascript?
    Thanks
    Ranco

  4. Giancarlo N

    29 Dec 2015 Administrator

    Interesting point, I think you cannot use the builtin GoToPage, maybe in the future you give us an idea :)

    The most simple solution coming up in mind is to create a global javascript variable called for example myEpubTotalPages and put it to project html header in this way:

    window['myEpubTotalPages'] = 6

    where 6 is the number of pages.

    then use it in the run javascript action, such as:

    var paths = window.location.pathname.split('/');
    var currPageNum = parseInt(paths[paths.length-2]);
    var newPage = Math.floor(Math.random() * (window['myEpubTotalPages'])) + 1;
    while (newPage == currPageNum) newPage = Math.floor(Math.random() * (window['myEpubTotalPages'])) + 1;
    window.location.href = '../'+newPage+'/page.xhtml';

    idea here is to jump to a page between 1 and "myEpubTotalPages" avoiding current page

  5. Cool, I'll try it, thanks!

  6. Tried it and it works great, thanks a lot!

 

or Sign Up to reply!