paging with JavaScript and ASP "Next" "Previous"

Discussion in 'Programming' started by gilgalbiblewheel, Jul 4, 2007.

  1. #1
    I have a 700 page website paged pg_0001 - pg_0700. And I figure to my knowledge in JavaScript and ASP ( unless you have an easier way ) I want to first read the existing pathname:

    document.write(location.pathname);
    Code (markup):
    Then somehow add +1 ( next page ) to the string or -1 ( previous page )

    	function prevLocation() {
    		document.write(location.pathname);
    		window.location="pg_0001<%'1%>.asp";
    	}
    	function nextLocation() {
    		document.write(location.pathname);
    		window.location="pg_0001<%'1%>.asp";
    	}
    Code (markup):
    <a href="#" onclick="prevLocation();">Previous Page</a>
    <a href="#" onclick="nextLocation();">Next Page</a>
    Code (markup):
     
    gilgalbiblewheel, Jul 4, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why use javascript? use the ASP to get the current pages number and have the link as current number +1 or current number -1
     
    AstarothSolutions, Jul 4, 2007 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Ok I have this so far:
    
    page=PageCount		
    page = Request.ServerVariables("script_name")
    
    Code (markup):
    And then:
    href="<%response.Write(Replace(page, "002", "001"))%>"
    Code (markup):
    Hmmm... I don't know how to replace the three digits with a +/- 1.
     
    gilgalbiblewheel, Jul 4, 2007 IP
  4. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #4
    if you have all the pages static, then javascript may be a good way to go.
    otherwise if you are retrieving the data from database, of course asp would be the better choice.
    you can do the paging in many ways:
    1. retrieve all data, save in a session(not good for lot of data) then loop through to find the websites on that page. You can use the pagenumber on a session or Viewstate variable.
    2. you can use a stored procedure to get the data only for the page you need. I suggest this one. to save the current page number best choice is viewstate. you can use session also.
     
    webcosmo, Jul 4, 2007 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    So What do you think of what I said above ^?
     
    gilgalbiblewheel, Jul 4, 2007 IP
  6. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #6
    Sure, thats good also. But you can do the whole think using javascript also without using asp.
    You can read the page number variable as you said, then call the document based on that.
     
    webcosmo, Jul 4, 2007 IP
  7. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    Ok how would you do that?
     
    gilgalbiblewheel, Jul 4, 2007 IP
  8. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #8
    hmm give me some time, i will work on that.
     
    webcosmo, Jul 4, 2007 IP
  9. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #9
    This is the code to get querystring. Using this, you can find the currentpage.

    //==============START OF QUERYSTRING FUNCTIONS
    function PageQuery(q)
    {
    if(q.length > 1)
    this.q = q.substring(1, q.length);
    else
    this.q = null;
    this.keyValuePairs = new Array();
    if(q)
    {
    for(var i=0; i < this.q.split("&").length; i++)
    {
    this.keyValuePairs = this.q.split("&");
    }
    }

    this.getKeyValuePairs = function()
    {
    return this.keyValuePairs;
    }

    this.getValue = function(s)
    {
    for(var j=0; j < this.keyValuePairs.length; j++)
    {
    if(this.keyValuePairs[j].split("=")[0] == s)
    return this.keyValuePairs[j].split("=")[1];
    }
    return false;
    }
    this.getParameters = function()
    {
    var a = new Array(this.getLength());
    for(var j=0; j < this.keyValuePairs.length; j++)
    {
    a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
    }
    this.getLength = function()
    {
    return this.keyValuePairs.length;
    }
    }

    //Call this function to get the querystring values
    function queryString(key)
    {
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
    }
    //============END OF QUERYSTRING FUNCTIONS

    function prevLocation() {
    if(queryString("currentPage"))
    {
    }
    window.location="pg_0001.asp";
    }
    function nextLocation() {
    window.location="pg_0002.asp";
    }

    //test
    alert(queryString("name"));

    //You have work on the Prev & NextLocation functions based on what you get on the querystring value.
    Hope that helps.
     
    webcosmo, Jul 6, 2007 IP
  10. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Where as we would do it all on ASP rather than javascript as you arent introducing a new journey back to the server and some people have javascript disabled therefore preventing your navigation from working

    If these are static pages, why arent you just putting the links in manually rather than trying to do it dynamically client or server side?
     
    AstarothSolutions, Jul 6, 2007 IP