We help IT Professionals succeed at work.

Next Page >>

cbradstreet
cbradstreet asked
on
10,055 Views
Last Modified: 2012-05-04
I use the following code to let the user click a Next Page link and take him from priceless0001.htm to priceless0002.htm. Tis works fine until he is on priceless0009.htm. When he clicks the Next page link on priceless0009.htm, he is taken back to priceless0001.htm. I need him to go to priceless0010.htm.

<script>
function nextPg() {
var str = window.location.href ;
//var stop = str.indexOf(".") ;
var stop = str.indexOf(".htm") ;
str = str.substring(stop-4,stop) ;
var index = parseInt(str) ;
index += 1 ;
if (index < 10) {
str = "000" + index;
} else if (index > 9 && index < 100) {
str = "00" + index;
} else if (index > 99 && index < 1000) {
str = "0" + index;
} else {
str = index
}
str = "priceless" + str + ".htm"
window.location = str
}
</script>

<table border="0" cellpadding="3" cellspacing="0">
<tr>
<td><span class="text">[<a href="javascript:history.back()" class="main">Last Pic</a><span class="text">]</TD>
<td><span class="text">[<a href="javascript:nextPg()" class="main">Next Pic</a><span class="text">]</TD>
</tr>
</table>
Comment
Watch Question

Commented:
Change
var index = parseInt(str) ;

to

var index = str * 1 ;
coded as regex going forwards

<script>
var str = 'priceless0009.htm'
num=str.match(/\d{4}/)[0]
num=1*num.match(/\d{4}$/)[0]+1
num=("0000"+num).match(/\d{4}$/)[0]
str=str.replace(/\d{4}/,num)
alert(str)
</script>

and backwards

<script>
var str = 'priceless0010.htm'
num=str.match(/\d{4}/)[0]
num=1*num.match(/\d{4}$/)[0]-1
num=("0000"+num).match(/\d{4}$/)[0]
str=str.replace(/\d{4}/,num)
alert(str)
</script>
or as a single function

<script>
function newPage(url,inc){
 num=url.match(/\d{4}/)[0]
 num=1*num.match(/\d{4}$/)[0]+inc
 num=("0000"+num).match(/\d{4}$/)[0]
 url=url.replace(/\d{4}/,num)
 return url
}

alert(newPage('http://mysite/priceless0009.htm',1))
alert(newPage('http://mysite/priceless0009.htm',2))
alert(newPage('http://mysite/priceless0010.htm',-1))
alert(newPage('http://mysite/priceless0005.htm',-1))
</script>

or even, sorry for all the posts (it is a cool problem)

<script>

function newPage(url,inc){
 num=url.match(/\d{4}\.htm/)[0].replace('.htm','')
 num=(1*num.match(/\d{4}$/)[0])+inc
 num=("0000"+num).match(/\d{4}$/)[0]
 url=url.replace(/\d{4}\.htm/,num+'.htm')
 return url
}

alert(newPage('http://mysite.com/folder0005/priceless0009.htm',1))
alert(newPage('http://mysite.com/folder0005/priceless0009.htm',2))
alert(newPage('http://mysite.com/folder0005/priceless0010.htm',-1))
alert(newPage('http://mysite.com/folder0005/priceless0005.htm',-1))
</script>
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Here my proposal:
<script>
function nextPg(step) {
  var str = window.location.href;
  pNum = str.match(/(\d+)\.htm/i)[1];
  pNum = pNum * 1 + step+'';
  if(pNum>0){
    pNum = "0000".substr(0, 4-pNum.length)+pNum;
    window.location = str.replace(/\d+\.htm/i, pNum+'.htm');
  }
}
</script>

<table border="0" cellpadding="3" cellspacing="0">
<tr>
<td><span class="text">[<a href="javascript:nextPg(-1)" class="main">Prev Pic</a><span class="text">]</TD>
<td><span class="text">[<a href="javascript:nextPg(+1)" class="main">Next Pic</a><span class="text">]</TD>
</tr>
</table>

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
change
var index = parseInt(str) ;
to
var index = parseInt(str,10) ;

which will treat the 0009 as a base 10 number instead of octal
mine of course would be implwemented as

<script>
function newPage(inc){
 window.location.href
 num=url.match(/\d{4}\.htm/)[0].replace('.htm','')
 num=(1*num.match(/\d{4}$/)[0])+inc
 num=("0000"+num).match(/\d{4}$/)[0]
 url=url.replace(/\d{4}\.htm/,num+'.htm')
 return url
}

alert(newPage(1))
alert(newPage(2))
alert(newPage(-1))
</script>
I mean

<script>
function newPage(inc){
 url=window.location.href
 num=url.match(/\d{4}\.htm/)[0].replace('.htm','')
 num=(1*num.match(/\d{4}$/)[0])+inc
 num=("0000"+num).match(/\d{4}$/)[0]
 url=url.replace(/\d{4}\.htm/,num+'.htm')
 return url
}

alert(newPage(1))
alert(newPage(2))
alert(newPage(-1))
</script>
Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
You can use this lpad function inside your nextpg function

function lpad(number, length, character)
{
  rtn="";
  rtn = number.toString();
  size = length - rtn.length;
  for (i=0; i < size; i++)
  {
    rtn = character + rtn;
  }

  return rtn;
}

nextPg function :

function nextPg() {
var str = window.location.href ;
var stop = str.indexOf(".htm") ;
str = str.substring(stop-4,stop) ;
var index = parseInt(str) ;
index += 1 ;
str = "priceless" + lpad(index, 4, "0") + ".htm" ;
window.location = str;
}
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Thanks.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.