SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2004
    Posts
    419
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Function / Hyperlink Query

    I have an ajax page, which spits out thumbnail images. The idea is that a user can click prev / next to scroll through the images.

    This is an example:
    http://jimpix.co.uk/ajax01/001.asp?cat=23#

    The prev / next links use a "MyCurrentVar" javascript variable, which is incremented / decremented each time, and sent to the ASP processing page, to retrieve the database info.

    I want though to restrict what the values of prev / next can be, to prevent the users endlessly using prev/next to generate useless output.

    So I thought I could use this in the javascript - use a function to control the values of "MyCurrentVar":

    Code:
    var MyCurrentVar = 1;
    
    function prev() {
    	MyCurrentVar--;
    
    	if (MyCurrentVar <= 1) {
    		MyCurrentVar = 1
    	}
    }
    
    function next() {
    	MyCurrentVar++;
    
    	if (MyCurrentVar >= 12) {
    		MyCurrentVar = 12
    	}
    }
    That sort of makes sense, but then when it comes to coding the HTML hyperlinks, how do I do that. E.g. I tried to do this for the hyperlinks:

    HTML Code:
    <a href="#" onclick="sendRequest(prev())">Prev</a> | <a href="#" onclick="sendRequestnext())">Next</a>
    But it doesn't work - because I need to send the value of the MyCurrentVar through. Hopefully it's just a syntax issue! Sorry for the dim question!

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your prev and next functions, try adding

    return MyCurrentVar;

    at the end.

  3. #3
    SitePoint Member
    Join Date
    Jan 2008
    Location
    Sydney, Australia
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As was touched on by the Wizard above, it looks like it may be a slight problem handling your variables/functions.

    If the function sendRequest expects a parameter, being the ID number for the next action, then having the prev and next functions perform "return MyCurrentVar;" as their last action will fix that.

    If the function sendRequest accessed MyCurrentVar directly, then it should execute properly as detailed above. (If you also remember to include the opening bracket before "next()".)

    So, short-short version:
    • If the sendRequest function is declared as function sendRequest( inCurrentVar ) {... then add that return line to the prev and next functions.
    • If the sendRequest function is getting the MyCurrentVar value directly, it should execute properly.


    If in doubt, post a link to an accessible test version and let us look at the code.

    Hope that helps.
    // Luke Stevenson
    // Lucanos Solutions


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •