//Ajax Pagination Script- Author: Dynamic Drive (http://www.dynamicdrive.com) //** Created Sept 14th, 07' //** Updated Oct 31st, 07'- Fixed bug when book only contains 1 page //** Updated Aug 9th 08'- Upgraded to v1.2: //1) Adds ability to limit the range of the visible pagination links shown for a book with many pages //2) Adds Session only persistence to persist the last page user viewed when navigating away then returning to the webpage containing the script. //3) Modified Ajax request function in IE7 to use ActiveXObject object, so script can be tested offline var ajaxpageclass=new Object() ajaxpageclass.loadstatustext="Hold on a second, we are loading the page..." // HTML to show while requested page is being fetched: ajaxpageclass.ajaxbustcache=false // Bust cache when fetching pages? ajaxpageclass.paginatepersist=true //enable persistence of last viewed pagination link (so reloading page doesn't reset page to 1)? ajaxpageclass.pagerange=4 // Limit page links displayed to a specific number (useful if you have many pages in your book)? ajaxpageclass.ellipse="..." // Ellipse text (no HTML allowed) /////////////// No need to edit beyond here ///////////////////////// ajaxpageclass.connect=function(pageurl, divId){ var page_request = false var bustcacheparameter="" if (window.XMLHttpRequest && !document.all) // if Mozilla, Safari etc (skip IE7, as object is buggy in that browser) page_request = new XMLHttpRequest() else if (window.ActiveXObject){ // if IE6 or below try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } catch (e){ try{ page_request = new ActiveXObject("Microsoft.XMLHTTP") } catch (e){} } } else return false document.getElementById(divId).innerHTML=this.loadstatustext //Display "fetching page message" page_request.onreadystatechange=function(){ajaxpageclass.loadpage(page_request, divId)} if (this.ajaxbustcache) //if bust caching of external page bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime() page_request.open('GET', pageurl+bustcacheparameter, true) page_request.send(null) } ajaxpageclass.loadpage=function(page_request, divId){ if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){ document.getElementById(divId).innerHTML=page_request.responseText } } ajaxpageclass.getCookie=function(Name){ var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair if (document.cookie.match(re)) //if cookie found return document.cookie.match(re)[0].split("=")[1] //return its value return null } ajaxpageclass.setCookie=function(name, value){ document.cookie = name+"="+value } ajaxpageclass.getInitialPage=function(divId, pageinfo){ var persistedpage=this.getCookie(divId) var selectedpage=(this.paginatepersist && this.getCookie(divId)!=null)? parseInt(this.getCookie(divId)) : pageinfo.selectedpage return (selectedpage>pageinfo.pages.length-1)? 0 : selectedpage //check that selectedpage isn't out of range } ajaxpageclass.createBook=function(pageinfo, divId, paginateIds){ //MAIN CONSTRUCTOR FUNCTION this.pageinfo=pageinfo //store object containing URLs of pages to fetch, selected page number etc this.divId=divId this.paginateIds=paginateIds //array of ids corresponding to the pagination DIVs defined for this pageinstance //NOTE: this.paginateInfo stores references to various components of each pagination DIV defined for this pageinstance //NOTE: Eg: divs[0] = 1st paginate div, pagelinks[0][0] = 1st page link within 1st paginate DIV, prevlink[0] = previous link within paginate DIV etc this.paginateInfo={divs:[], pagelinks:[[]], prevlink:[], nextlink:[], previouspage:null, previousrange:[null,null], leftellipse:[], rightellipse:[]} this.dopagerange=false this.pagerangestyle='' this.ellipse='' //construct HTML for ellipse var initialpage=ajaxpageclass.getInitialPage(divId, pageinfo) this.buildpagination(initialpage) this.selectpage(initialpage) } ajaxpageclass.createBook.prototype={ buildpagination:function(selectedpage){ //build pagination links based on length of this.pageinfo.pages[] this.dopagerange=(this.pageinfo.pages.length>ajaxpageclass.pagerange) //Bool: enable limitpagerange if pagerange value is less than total pages available this.pagerangestyle=this.dopagerange? 'style="display:none"' : '' //if limitpagerange enabled, hide pagination links when building them this.paginateInfo.previousrange=null //Set previousrange[start, finish] to null to start if (this.pageinfo.pages.length==0) return else if (this.pageinfo.pages.length==1) var paginateHTML="Page 1 of 1" //Pagination HTML to show when there's only 1 page (no pagination needed) else{ //construct paginate interface var paginateHTML='