cbradstreet
asked on
Next Page >>
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.b ack()" 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>
<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.b
<td><span class="text">[<a href="javascript:nextPg()"
</tr>
</table>
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}/,nu m)
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}/,nu m)
alert(str)
</script>
<script>
var str = 'priceless0009.htm'
num=str.match(/\d{4}/)[0]
num=1*num.match(/\d{4}$/)[
num=("0000"+num).match(/\d
str=str.replace(/\d{4}/,nu
alert(str)
</script>
and backwards
<script>
var str = 'priceless0010.htm'
num=str.match(/\d{4}/)[0]
num=1*num.match(/\d{4}$/)[
num=("0000"+num).match(/\d
str=str.replace(/\d{4}/,nu
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}/,nu m)
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>
<script>
function newPage(url,inc){
num=url.match(/\d{4}/)[0]
num=1*num.match(/\d{4}$/)[
num=("0000"+num).match(/\d
url=url.replace(/\d{4}/,nu
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].repla ce('.htm', '')
num=(1*num.match(/\d{4}$/) [0])+inc
num=("0000"+num).match(/\d {4}$/)[0]
url=url.replace(/\d{4}\.ht m/,num+'.h tm')
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>
<script>
function newPage(url,inc){
num=url.match(/\d{4}\.htm/
num=(1*num.match(/\d{4}$/)
num=("0000"+num).match(/\d
url=url.replace(/\d{4}\.ht
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>
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>
<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
<td><span class="text">[<a href="javascript:nextPg(+1
</tr>
</table>
change
var index = parseInt(str) ;
to
var index = parseInt(str,10) ;
which will treat the 0009 as a base 10 number instead of octal
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].repla ce('.htm', '')
num=(1*num.match(/\d{4}$/) [0])+inc
num=("0000"+num).match(/\d {4}$/)[0]
url=url.replace(/\d{4}\.ht m/,num+'.h tm')
return url
}
alert(newPage(1))
alert(newPage(2))
alert(newPage(-1))
</script>
<script>
function newPage(inc){
window.location.href
num=url.match(/\d{4}\.htm/
num=(1*num.match(/\d{4}$/)
num=("0000"+num).match(/\d
url=url.replace(/\d{4}\.ht
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].repla ce('.htm', '')
num=(1*num.match(/\d{4}$/) [0])+inc
num=("0000"+num).match(/\d {4}$/)[0]
url=url.replace(/\d{4}\.ht m/,num+'.h tm')
return url
}
alert(newPage(1))
alert(newPage(2))
alert(newPage(-1))
</script>
<script>
function newPage(inc){
url=window.location.href
num=url.match(/\d{4}\.htm/
num=(1*num.match(/\d{4}$/)
num=("0000"+num).match(/\d
url=url.replace(/\d{4}\.ht
return url
}
alert(newPage(1))
alert(newPage(2))
alert(newPage(-1))
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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;
}
Thanks.
var index = parseInt(str) ;
to
var index = str * 1 ;