SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    1 Post(s)
    0 Thread(s)

    I need scripted Next and Prev buttons to flip through pages

    I have a series of pages (e.g. page1.html, page2.html, page3.html, page4.html) and I need a javascript to generate next and prev buttons.

    In order to do this I'm using a script that goes something like this (it doesn't work so this is just the idea of it).

    Code JavaScript:
          document.write('<input type=button value="Next" onclick="this.href='page2.html'">');

    There are 2 issues I'm having.

    The first is that unless I put the full url (e.g. "C://web/page1.html") the document.write doesn't run. I need to be able to test the actual page reference, not it's full path as this is local and will be uploaded.

    The second is how to get the changing next button href to essentially be page1.html + 1, page[n].html +1 if you like.

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    0 Post(s)
    0 Thread(s)
    You shouldn't have navigation that depends on javascript entirely, some people disable javascript in their browser.
    Maybe you can use PHP for this, so you can be sure the buttons are displayed.

    Code PHP:
        if (eregi(".*page([0-9]+)\.html$", __FILE__, $regs)) {
            echo '<input type="button" value="Previous" onclick="this.href=\'page'.($regs[1]-1).'.html\';">';
            echo '<input type="button" value="Next" onclick="this.href=\'page'.($regs[1]+1).'.html\';">';

    My first guess is that you're using static pages that actually have dynamic content, so the set up is not how I would do it.
    If you use dynamic pages (content provided through PHP and MySQL for example), you can use the RewriteEngine (htaccess) to make the urls 'static'.

    For example mypage.php?page=2 would become page2.html in your browser.
    The file mypage.php contains some PHP to write the content on that page.

    An example of a .htaccess file
    RewriteEngine On
    RewriteRule ^page([0-9]+)\.html$ mypage.php?page=$1
    My explenation is very brief, but it should get you going.
    FOR SALE: 1 set of morals, never used, will sell cheap


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts