Pure JS Paging code (simple and practical) _javascript skills

Source: Internet
Author: User

Copy Code code as follows:

Display words per page
pagesize=5000;
Paging mode
FLAG=2;//1: According to the word automatic page 2: According to [NextPage] pagination
Default page
StartPage = 1;
Navigation display style 0: General 1: Direct 3: Dropdown
Topshowstyle = 1;
Downshowstyle = 0;

var currentset,cutflag,totalbyte,pagecount,key,temptext,temppage;
Key= "";
currentset=0;
var text=xmlarticle.selectsinglenode ("//content"). Text;
Totalbyte=text.length;

if (flag==1)
{
Pagecount=math.round (totalbyte/pagesize);
if (Parsefloat ("0.") +totalbyte%pagesize) >0) {
if (Parsefloat ("0.") +totalbyte%pagesize) <0.5) {
pagecount=pagecount+1;
}
}
var pagenum=new Array (pagecount+1);
var pagetitle=new Array (pagecount+1);
pagenum[0]=0;
Pagetitle[0]= "";

var Sdrv1,sdrv2,sdrv3,sdrv4,sflag;
var sdrvl,steml;
var stem1,stem2,k;
sflag=0;

for (j=1;j<pagecount+1;j++) {
Pagenum[j]=pagenum[j-1]+pagesize;
Pagetitle[j]= "";
Alert (j);
Sdrv1= "<br>";
Sdrv2= "<BR>";
Sdrv3= "<Br>";
Sdrv4= "<bR>";
Sdrvl=sdrv1.length;
for (k=pagenum[j];k<=totalbyte;k++) {
Stem1=text.substring (PAGENUM[J]-SDRVL,K);
Steml=stem1.length;
Stem2=stem1.substring (STEML-SDRVL,STEML)
if (Stem2==sdrv1 | | stem2==sdrv2 | | stem2==sdrv3 | | stem2==sdrv4)
{
sflag=sflag+1;
Pagenum[j]=k;
Break
}
}
if (pagenum[j]>totalbyte)
{
Break
}
}
if (J<pagecount)
{
Pagenum.length=j;
Pagecount=j
}
if (Pagecount>1&&sflag>1&&pagecount<sflag)
{
pagecount=sflag+1;
}
}
else{
Manual page-Splitting
var J,sflag,pagecount,stext;
var Stitleflag;
var pagenum=new Array ();
var pagetitle=new Array ();

pagesize=0;
J=1;
pagenum[0]=-10;
Pagetitle[0]= "";
sflag=0;
Stext=text;

Todo
{
Stext=text.substring (Pagenum[j-1]+10,totalbyte);

Sflag=stext.indexof ("[NextPage");

if (stext.substring (sflag+9,sflag+10) = "=")
{
Stitleflag=stext.indexof ("]", sflag);
Pagetitle[j]=stext.substring (Sflag+10,stitleflag);
}
else{
Pagetitle[j]= "";
}

if (sflag>0)
{
pagenum[j]=sflag+pagenum[j-1]+10;
}
else{
Pagenum[j]=totalbyte;
}

J+=1;
}
while (Pagenum[j-1]<totalbyte);

Pagecount=j-1;
}

function Text_pagination (Page) {
var output,byte;

if (page==null) {page=1;}

Output= "";
output=output+ "<table width=100% height=30 border=0 align=center cellpadding=0 cellspacing=0>";
output=output+ "<tr>";
output=output+ "<td height=1 background=images/dotline.gif></td>";
output=output+ "</tr>";

Head function navigation bar
output=output+ "<tr>";

Body Lookup

output=output+ "<td align=left bgcolor= #f0faff width= ' 40% ' >";
output=output+ "<input type=text name=keys onchange= ' key=this.value ' size=12> <input Type=button ' Name=search Value= ' Find the body ' onclick= ' searchkey (); ' style= ' width:60 ' > ';
output=output+ "</td>";

output=output+ "<td align=right bgcolor= #f0faff >";

Page number display mode one
Page x: pagination title
if (page==0 | | pagecount==0) {
Output=output+ "Current is: <font color=red> full text display </font>";
}
else{
if (totalbyte>pagesize) {byte=pagenum[page]-pagenum[page-1]}else{byte=totalbyte};
output=output+ "<font color=red>" +page+ "</font> page";
if (pagetitle[page]!= "")
{
output=output+ ": <font color=800000>" +pagetitle[page]+ "</font>";
}
output+= ';
}

Display Mode two
Drop-down Menu Selection
if (pagecount>0)
//{
Output=output+article_pagenav (2,page);
output=output+ "</td>";
//}

Display Mode three
Page number selection list
output=output+ "<td align=right bgcolor= #f0faff >";
Output=output+article_pagenav (0,page);
output=output+ "</td>";

output=output+ "</tr>";
output=output+ "<tr>";
output=output+ "<td height=1 background=images/dotline.gif></td>";
output=output+ "</tr>";
output=output+ "</table>";

Show Body
if (page==0) {
No paging
Temptext=text;
}
else{
Paging
if (flag==1)
Automatic page splitting
{
Temptext=text.substring (Pagenum[page-1],pagenum[page]);
}
else{
Manual page-Splitting
if (pagetitle[page-1].length==0)
{
Temptext=text.substring (Pagenum[page-1]+10,pagenum[page]);
}
else{
Temptext=text.substring (Pagenum[page-1]+11+pagetitle[page-1].length,pagenum[page]);
}
}
}

Layout content
output=output+ "<div align=center>";
Output=output+article_pagenav (Topshowstyle,page);
output=output+ "</div>";

output=output+ "<div id=world>";
Output=output+temptext;
output=output+ "</div>";
output=output+ "<br>";

output=output+ "<div align=center>";
Output=output+article_pagenav (Downshowstyle,page);
output=output+ "</div>";

article.innerhtml = Output;
if (page>1)
{
document.location.href= ' #top ';
}

Eval (document.all.keys). Value=key;
if (key!= "") {Searchkey ();}
}

function Searchkey () {
Body Lookup function

H= "<font class=keyworld>";
F= "</font>";
Keyset=new Array ();
Key=document.all.keys.value;
if (key== "") {
Alert ("Please enter the keyword!") ");
Return
}
else{
Keyset[0]=temptext.indexof (key,0);
if (keyset[0]<0) {
Return
}else
Temp=temptext.substring (0,keyset[0]);
Temp=temp+h+key+f;
Temp2=temptext.substring (keyset[0]+key.length,temptext.length);
for (i=1;i<temptext.length;i++) {
Keyset[i]=temptext.indexof (key,keyset[i-1]+key.length);
if (keyset[i]<0) {
Temp=temp+temptext.substring (keyset[i-1]+key.length,temptext.length);
Break
}else{
Temp=temp+temptext.substring (Keyset[i-1]+key.length,keyset[i]) +h+key+f;
}
}
world.innerhtml = temp;
}
}

function Article_pagenav (showstyle,page) {
Page-Numbering display function
parameter is call style, 0 = simple style, 1= standard style
var temp= "";

if (showstyle==0)
Simple style
{
Temppage=page;
if (totalbyte>pagesize) {
if (page-4<=1) {
temp=temp+ "<font face=webdings color= #999999 >9</font>";
if (page<=1) {temp=temp+ "<font face=webdings color= #999999 >7</font>";} else{temp=temp+ "<a href=javascript:text_pagination (" + (Page-1) + ") ><font face=webdings>7</font> </a> ";}
if (pagecount>10) {
for (i=1;i<8;i++) {
if (i==page) {
temp=temp+ "<font color=red>" +i+ "</font>";
}else{
temp=temp+ "<a href=javascript:text_pagination (" +i+ ") >" +i+ "</a>" + "";
}
}
Temp=temp+ "...";
}
else{
for (i=1;i<pagecount+1;i++) {
if (i==page) {
temp=temp+ "<font color=red>" +i+ "</font>";
}
else{
temp=temp+ "<a href=javascript:text_pagination (" +i+ ") >" +i+ "</a>" + "";
}
}
}

if (page==pagecount) {temp=temp+ "<font face=webdings color= #999999 >8</font>";} else{temp=temp+ "<a href=javascript:text_pagination (" + (page+1) + ") ><font face=webdings>8</font> </a> ";}
if (pagecount<10) {temp=temp+ "<font face=webdings color= #999999 >:</font>";} else{temp=temp+ "<a href=javascript:text_pagination (" +pagecount+ ") ><font face=webdings>:</font> </a> ";}
}
else if (Page+4<=pagecount) {
temp=temp+ "<a href=javascript:text_pagination (1) ><font face=webdings>9</font></a>";
temp=temp+ "<a href=javascript:text_pagination (" + (Page-1) + ") ><font face=webdings>7</font></a > ";
if (pagecount>10) {
Temp=temp+ "..";
for (i=page-4;i<page+4;i++) {
if (i==page) {
temp=temp+ "<font color=red>" +i+ "</font>";
}
else{
temp=temp+ "<a href=javascript:text_pagination (" +i+ ") >" +i+ "</a>" + "";
}
}
Temp=temp+ "..";
}
else{
for (i=1;i<pagecount+1;i++) {
if (i==page) {
temp=temp+ "<font color=red>" +i+ "</font>";
}
else{
temp=temp+ "<a href=javascript:text_pagination (" +i+ ") >" +i+ "</a>" + "";
}
}
}

if (page==pagecount) {temp=temp+ "<font face=webdings color= #999999 >8</font>";} else{temp=temp+ "<a href=javascript:text_pagination (" + (page+1) + ") ><font face=webdings>8</font> </a> ";}
temp=temp+ "<a href=javascript:text_pagination (" +pagecount+ ") ><font face=webdings>:</font></ A> ";

}
else{
temp=temp+ "<a href=javascript:text_pagination (1) ><font face=webdings>9</font></a>";
temp=temp+ "<a href=javascript:text_pagination (" + (Page-1) + ") ><font face=webdings>7</font></a > ";
Temp=temp+ "..."

for (i=page-2;i<pagecount+1;i++) {
if (i==page) {
temp=temp+ "<font color=red>" +i+ "</font>";
}
else{
temp=temp+ "<a href=javascript:text_pagination (" +i+ ") >" +i+ "</a>" + "";
}
}

if (page==pagecount) {temp=temp+ "<font face=webdings color= #999999 >8</font>";} else{temp=temp+ "<a href=javascript:text_pagination (" + (page+1) + ") ><font face=webdings>8</font> </a> ";}
temp=temp+ "<font face=webdings color= #999999 >:</font>";
}
}
else{
temp=temp+ "<font color=red>1</font>";
}

temp=temp+ "<a href=javascript:text_pagination (0) > Show All </a>"
}
else if (showstyle==1)
Standard Style
{
if (totalbyte>pagesize) {if (page!=0) {if (page!=1) {temp=temp+ <a href= ' #top ' Onclick=javascript:text_ Pagination ("+ (Page-1) +") ><font color=3366cc>[previous page]</font></a> ";}}}
for (i=1;i<pagecount+1; i++)
{
if (page==i)
{
temp=temp+ "<font color=800000>[" +i+ "]</font>";
}
else{
temp=temp+ "<a href= ' #top ' onclick=javascript:text_pagination (" +i+ ") ><font color=3366cc>[" +i+ "]</ Font></a> ";
}
}
temp=temp+ "<a name= ' foot ' ></a>";
if (totalbyte>pagesize) {if (page!=0) {if (page!=pagecount) {temp=temp+ <a href= ' #top ' Onclick=javascript:text_ Pagination ("+ (page+1) +") ><font color=3366cc>[next page]</font></a> ";}}}

   temp=temp+ "<a href=javascript:text_pagination (0) ><font color=3366cc> Show All </font> </a> '
 }
  Else if (showstyle==2)
 //dropdown menu style
  {
   temp=temp+ ' <select onchange= "text_pagination (this.value)" > "
   for (i=1;i<pagecount+1; i++)
    {
    if (page==i)
    {
     temp=temp+ "< Option value= ' +i+ ' selected style= ' color:red ' > ' +i+ ' page '

   }
    else{
     temp=temp+ "<option value= '" +i+ "' >" +i+ "page";
   }
    if (pagetitle[i].length!=0)
    {
     temp=temp+ ' : ' +pagetitle[i];
   }
    temp=temp+ "</option>";
  }
   temp=temp+ "</select>";
 }

return (temp);
}

Default page
Text_pagination (StartPage);


--------------Paging JS code end--------------

------------HTML page, call paging JS------------------

Copy Code code as follows:

<meta http-equiv=content-type content= "text/html; charset=gb2312 ">
</HEAD>
<body bottommargin=0 leftmargin=0 topmargin=0 rightmargin=0>

Cellspacing=1 cellpadding=5 width= "<table" Align=center
Border=0>
<TBODY>
<TR>
<TD>

<xml id=xmlarticle>
<Article>
<Info>
<Content>
<! [cdata[
Everyone to test the page Oh ~~~~~[nextpage] I divided ~~~[nextpage] I divided the [NextPage] cent
]]>
</Content>
</Info>
</Article>
</xml>

<!--body pagination js-->
<script Language=javascript
Src= "Attachments/month_0607/j200674214834.js" ></SCRIPT>


</TD></TD></TR></TBODY></TABLE></TD></TR>
</TBODY></TABLE>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.