PDA

View Full Version : Looking for a command for "go to next page alphabetically".



bluenew
02-05-2016, 04:20 AM
Looking for a Java or Html5 command that would "Check self" and then go to the next page alphabetically (or numerically).

Example: html pages as follows -- 011.html, 012.html, 013.html, 021.html, 022.html, ... If user is on page 013.html clicks forward button, user would be taken to 021.html.

I have spent hours searching the web and forums but have not found anything.
Maybe this is not possible, but if you have any ideas I'd appreciate it.

Beverleyh
02-05-2016, 06:35 AM
You would have to build a function yourself.

Increment characters http://stackoverflow.com/questions/12504042/what-is-a-method-that-can-be-used-to-increment-letters

Increment numbers - look into the increment (++) operator. Something like;
var $i = 0;
i++;
// $i now equals 1

If you post an example of what you're trying to achieve, and your JavaScript so far, maybe somebody can offer more help.

styxlawyer
02-05-2016, 11:03 AM
The browser will have no information about what other pages are available on the server, so this will probably have to be a server side PHP page which is called from the current page with a parameter. The PHP can then search the server (or preferably extract the name from a pre-built list file) for the next page in the sequence. For speed and simplicity I would use a list, but YMMV. The disadvantage is that the list will have to be updated every time a new page is added. You could have a cron job running on the server regularly to update the list of pages to save the manual updating.

Beverleyh
02-05-2016, 11:59 AM
The browser will have no information about what other pages are available on the server, so this will probably have to be a server side PHP page which is called from the current page with a parameter..

JavaScript has this option to grab the page from the URL;
var parts = window.location.pathname;
var part_array = parts.split( '/' );
var last_part = part_array.pop();
alert(last_part); // returns 'page.htm'

styxlawyer
02-05-2016, 12:21 PM
JavaScript has this option to grab the page from the URL;
.
.

Indeed it has, but that only returns the name of the current page in the browser. Javascript code has no direct access to the server and cannot provide information about what other pages are available. Only server-side scripting is capable of that.

molendijk
02-05-2016, 03:23 PM
You could try this:
On all pages:
<script src="alph.js"></script>
where alph.js is this:
var pages = ["011.html", "012.html", "013.html", "021.html","022.html", "023.html"];

function prev()
{
for (i = 0; i < pages.length; i++) {if(pages[i]==document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)&&pages[i-1]){window.location.href=pages[i-1]}}
}

function next()
{
for (i = 0; i < pages.length; i++) {if(pages[i]==document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)&&pages[i+1]){window.location.href=pages[i+1]} }
}

Then:
011.html:
<head>
<script src="alph.js"></script>
</head>

<body>
<h3 style="text-align: center">011.html</h3>
<span onclick="prev()">PREV</span>
<span onclick="next()">NEXT</span>
</body>


012.html:
<head>
<script src="alph.js"></script>
</head>

<body>
<h3 style="text-align: center">012.html</h3>
<span onclick="prev()">PREV</span>
<span onclick="next()">NEXT</span>
</body>

...And so on.

styxlawyer
02-05-2016, 03:53 PM
That would be great except that every time a new page is added to the server or an old page deleted then "alph.js" would also need to be updated. Also, there is a problem when the page is 011.html or 023.html as the PREV and NEXT respectively buttons would produce out of array errors.

molendijk
02-05-2016, 04:19 PM
On 011.html we could just make the PREV-button invisible. Similar solution for 023.html.
But yes, you're right about alph.js. I wouldn't know of another method to tell what pages are on the server.

Beverleyh
02-05-2016, 04:27 PM
Indeed it has, but that only returns the name of the current page in the browser. Javascript code has no direct access to the server and cannot provide information about what other pages are available. Only server-side scripting is capable of that.

Yes, I'm aware of that. However we don't know that the OP can use PHP as an option, and without further advice as to the nature of the project, I am only offering suggestions as to JavaScript's capabilities since that is the language asked for by the OP.

Beverleyh
02-05-2016, 04:30 PM
That would be great except that every time a new page is added to the server or an old page deleted then "alph.js" would also need to be updated. Also, there is a problem when the page is 011.html or 023.html as the PREV and NEXT respectively buttons would produce out of array errors.

Going back to my first post - we really could do with more information from the OP. Otherwise, what have been offered so far are all valid suggestions in the unknown.

It would probably be best to wait and see what the OP comes back with.