PDA

View Full Version : Page numbering system


mmasci
01-09-2003, 03:42 AM
I want to create a JavaScript function that will return a value based on the HTML page that it is dropped in.

Background.

I am using a Dreamweaver MX Template for a series of 15 HTML pages. I want to create a counter within this template that will say "Page x of 15" based on which HTML page is loaded/being viewed.

I understand that I will need to contruct an array for the 15 HTML pages, but cannot conceptualise the process any further.

I know that this mey be easier to do in other languages, but I would prefer to be able to do this in JavaScript.

Please help.

Regards,

mmasci.

jalarie
01-09-2003, 03:54 PM
Files=new Array(
 'first.htm',
 'second.htm',
 '....htm',
 'last.htm'
);
Loc=document.location.href;
Ix1=Loc.lastIndexOf('/');
File=Loc.substring(Ix1+1);
for&nbsp;(Ix1=0;&nbsp;Ix1<Files.length;&nbsp;Ix1++)&nbsp;{
&nbsp;if&nbsp;(Files[Ix1]&nbsp;==&nbsp;File)&nbsp;{
&nbsp;&nbsp;Ix2=Ix1+1;
&nbsp;&nbsp;alert('Page&nbsp;'+Ix2+'&nbsp;of&nbsp;'+Files.length+'.');
&nbsp;}
}

mmasci
01-09-2003, 11:32 PM
I have taken your code and interpreted it the following way. I want the output written in the text of the screen, not as an alert. For some reason though it doesn't seem to be working. It is not writing anything on the screen, not even the text in the document.write line.

Here is the code.

<script language="JavaScript">
function pageNumber() {
var Files = new Array(13)
Files[0]="intro.htm"
Files[1]="generalWHS.htm"
Files[2]="legislation.htm"
Files[3]="policy.htm"
Files[4]="incidents.htm"
Files[5]="WHScommittee.htm"
Files[6]="WHSdepartment"
Files[7]="workerscomp.htm"
Files[8]="infection.htm"
Files[9]="chemicals.htm"
Files[10]="manualhandling.htm"
Files[11]="emergency.htm"
Files[12]="thankyou.htm"

var Loc=document.location.href;
var Ix1=Loc.lastIndexOf('/');
var File=Loc.substring(Ix1+1);

for (Ix1=0; Ix1<Files.length; Ix1++) {
if (Files[Ix1] == File) {
var Ix2=Ix1+1;
document.write("Page " +Ix2+ " of " +Files.length+ ".");
}
}
}
</script>

what is wrong with this code??

mmasci.

whammy
01-10-2003, 12:04 AM
Hi,

This old script I wrote to automatically navigate between pages is a bit different, as it uses pages numbered from 0-whatever or whatever-whatever to work, but perhaps it may give you an idea.

Of course, you could modify it to use an array of pages that were named as you stated above.

http://www.solidscripts.com/displayscript.asp?sid=9

:)

mmasci
01-10-2003, 12:33 AM
Whammy,

Thanks for the link to that script! It looks very useful, and I'll probably adapt it with an upcoming project.

Unfortunately, it is not quite what I'm after here. I am only after a page counter here , not a navigation system as such. Ive already got that part of it in place.

Is there any other scripts you can suggest or ways in which pages can be assigned a number based on the order in which they are placed in the array?

Cheers,

mmasci

Borgtex
01-10-2003, 01:12 AM
Originally posted by mmasci
For some reason though it doesn't seem to be working. It is not writing anything on the screen, not even the text in the document.write line.


Maybe it's an stupid question... but are you calling the function pageNumber() in some way inside the page?

mmasci
01-10-2003, 01:17 AM
This is the way in which I am trying to call the script.

I am sourcing the script externally,

<script language="JavaScript" src="../pageNumber.js"></script>

and then calling it as an onload in the <body> tag.

I've then inserted a
<script>
document.write.... (cut and pasted from the above function)
</script>.

I'm not sure if the onLoad in the <body> tag is hte right way to call it.

What are your thoughts???

Cheers,

mmasci.

Borgtex
01-10-2003, 01:32 AM
First of all, use this script instead yours, if you want a page with an anchor or parameters (intro.htm#one - intro.htm?var=one) to be recognized


<script language="JavaScript">
function pageNumber() {
var Files = ["intro.htm" ,"generalWHS.htm","legislation.htm","policy.htm","incidents.htm","WHScommittee.htm","WHSdepartment","workerscomp.htm","infection.htm","chemicals.htm","manualhandling.htm","emergency.htm","thankyou.htm"]

var File=document.location.href;
for (Ix1=0; Ix1<Files.length; Ix1++)
{
if (File.indexOf('/'+Files[Ix1])!=-1)
{
var Ix2=Ix1+1;
document.write("Page " +Ix2+ " of " +Files.length+ ".");
}
}
}
</script>


second:don't call the function from the body tag, or it will overwrite the intere page. Instead:


<head>
..
...
<script language="JavaScript" src="../pageNumber.js"></script>
</head>

<body>
<!--
your stuff here
..
..
-->
<script>pageNumber()</script>
..
..
..
</body>

Ok?

jalarie
01-10-2003, 03:49 PM
mmasci, don't use a function. Just put the code, including the script tags, within the body of your page right where you want that message displayed.

mmasci
01-12-2003, 09:58 PM
Thanks for alll your help everyone!!

It is now up and running and working like a charm!!!

Cheers,

mmasci.