0

Alright so I am not the most astute javascript/jQuery user, but here it goes.

I want to make a series of html pages that will each link to the next/prev page and will loop once they reach the end of those pages ('page1' and 'lastpage.html'). I know I could do that with just html, but I don't want to have to continually go and revise each page, as I plan on adding pages to that bank/series of pages, so the amount of pages cycling will increase.

The one solution I have thought up of is to test if the 'next' link will exist like this

$(document).ready(function(e) {
$('#next').click(function(){
            .preventDefault();
    .ajax({
        type: 'HEAD',
        url: 'art_2.html',
        success: function() {
                window.location.href = "art_2.html";
        },
        error: function() {
                window.location.href = "art_1.html";
        }
    });
});
});

So basically how do I make this work, or am I trying to use a tool where I could be using a power-tool?

also added in a prevent default just remembered...

and guess what? Cannot use PHP :/ so yeah...

ALSO I'm kinda lack necessary intelligence to do a good deal of the things with the coding. Entirely self taught...

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • Since you're using AJAX, don't you wan't to use PHP? – TheFrozenOne Sep 01 '14 at 21:44
  • 1
    To be honest this looks like a succinct solution, when I started reading your question I though "well I'd probably do something with AJAX and 'HEAD' ..." and there it is :-) PS modern jQuery you can use done rather than success - see this question http://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done – splig Sep 01 '14 at 21:46
  • @Doodlebunch can't use PHP, server I'm on doesn't support it... – user2222936 Sep 01 '14 at 21:50

2 Answers2

0

This is a broken "fix" at most. You should have a server-side script that updates the list regardless of which page you are viewing.

An example of decent implementation would be a mySQL database with a table that contains each page id (auto increment), page url, and optionally a page name. Then dynamically create the "next" and "previous" page based on the results returned form the sql queries.

Chad
  • 1,531
  • 3
  • 20
  • 46
  • Well, I know that going into this, but let's say I have no idea what a "mySQL database" is and have no idea how to implement one :D – user2222936 Sep 01 '14 at 21:56
  • In that case, you'd look into learning it to save yourself a lot of hassle down the road. [php with mysql](http://www.w3schools.com/php/php_ref_mysqli.asp) and [sql](http://www.w3schools.com/sql/default.asp) – Chad Sep 01 '14 at 21:59
  • It is not necessary, but you have to communicate with your database somehow. If not php, you would have to do a different language that can handle sql connections. It may take you a decent bit of hard work and focus, but trust me, it will be worth it in the long-haul. – Chad Sep 01 '14 at 22:14
0
  1. Keep all pages you want in the cycle/loop in a folder in project
  2. Write a class that reads directory structure
  3. Manipulate file names as you want to link name and store these into a data structure
  4. In UI, based on current page name, get the next one.

I hope this helps.

You could store the data structure in session to access via UI.

Sri
  • 63
  • 1
  • 1
  • 8
  • You have any links that might have a tutorial or example of this? – user2222936 Sep 01 '14 at 23:36
  • That was from on top of my head as a solution to the problem you posted :-) What I can think of is a utility class that does this work for you, and returns you an instance of the data structure with page names. Invoke this utility class method in the action class (assuming you are using Struts), and you have the object with files present in that folder for you to construct your rotation logic. – Sri Sep 01 '14 at 23:57