Connecting Tech Pros Worldwide Forums | Help | Site Map

Forward-Back menu bar in frames - numbered set of pages

Chris
Guest
 
Posts: n/a
#1: Jul 23 '05
I have a script that will navigate back and foreward using a form. The
files it navigates are named: bg1.html, bg2.html, bg3.html, etc.

This is happening in a frameset, where my BACK and FORWARD buttons are
in one frame, and the content pages are in another (main).

I can get read the current page location in the frame "main", but it
is not working (getpagenumber)- it should give the current page
number, and use that number to tell the rest of the script where Back
and Foreward is. The current page is "bg1.html"

My script is below, followed by the form that will use it. Any help or
advice would be appreciated:

<script language="JavaScript">
<!--
function getpagenumber() {
var otherpage = parent.main.location.href;
var pagenumber =
otherpage.substring(otherpage.length-3,otherpage.length);
return pagenumber - 0;
}

function goForward() {
var page = getpagenumber();
if (page < 100)
parent.main.location.href = 'bg' + pad(page + 1,3) + '.html';
}
function goBackward() {
var page = getpagenumber();
if (page > 1)
parent.main.location.href = 'bg' + pad(page - 1,3) + '.html';
}
function pad(number,length) {
var str = '' + number;
while (str.length < length)
str = '0' + str;
return str;
}
//--></script>




<form>
<input type="button" value="Back" onClick="goBackward()">
<input type="button" value="Forward" onClick="goForward()">
</form>

Lee
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Forward-Back menu bar in frames - numbered set of pages


Chris said:[color=blue]
>
>I have a script that will navigate back and foreward using a form. The
>files it navigates are named: bg1.html, bg2.html, bg3.html, etc.[/color]
[color=blue]
><script language="JavaScript">
><!--
>function getpagenumber() {
> var otherpage = parent.main.location.href;
> var pagenumber =
>otherpage.substring(otherpage.length-3,otherpage.length);[/color]

alert(pagenumber);
[color=blue]
> return pagenumber - 0;
>}[/color]

Ignoring the problems involved with frames and the fact that
your <script> tag and comment lines are out of date, the first
step in debugging this script should be to add an alert() as
shown above to see what value you're getting for pagenumber.
That should tell you what you need to fix.

Richard Cornford
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Forward-Back menu bar in frames - numbered set of pages


Chris wrote:
<snip>[color=blue]
> ... named: bg1.html, bg2.html, bg3.html, etc.[/color]
<snip>[color=blue]
> <script language="JavaScript">
> <!--[/color]

The usual comments about the deprecated language attribute and the
outdated HTML comment script hiding practice applies to the above.
[color=blue]
> function getpagenumber() {
> var otherpage = parent.main.location.href;
> var pagenumber =
> otherpage.substring(otherpage.length-3,otherpage.length);[/color]

This substringing operation should return the last 3 characters in the
page's URL. Assuming no query string or fragment identifier, that should
be "tml" for a file with a ".html" suffix.
[color=blue]
> return pagenumber - 0;[/color]

Subtracting zero form the string "tml" will force a type conversion of
the string to a number prior to the subtraction. "tml" is no a number so
the result will be NaN, and all subsequent mathematical operations
acting on NaN will themselves return NaN.
[color=blue]
> }
>
> function goForward() {
> var page = getpagenumber();
> if (page < 100)[/color]

NaN is never less than 100.
[color=blue]
> parent.main.location.href = 'bg' + pad(page + 1,3) + '.html';
> }
> function goBackward() {
> var page = getpagenumber();
> if (page > 1)[/color]
<snip>

And NaN is never greater than 1.

Richard.


Closed Thread