SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    AJAX Pagination number click event using A tag

    Hai folks

    i have a small report generated at the bottom of a page.
    it has pagination numbers. all these ajax generated.
    now the pagination numbers have the following code.

    Code:
    echo "<a href=\"#\" id='$i' class='pagenum' onclick='ajaxpost_rep_gen($i);change($i)'/>" . $i . "</a>" . "&nbsp;&nbsp;";
    notice that for hreaf i have put #.

    now my problem is, when click a pagination number, the page is scrolled to the top of the page.
    so each time when click a pagination number at the bottom, user have to scroll down to see the report's next page.

    screen shot of the report :

    Attached Images Attached Images

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't have a lot of experience with inline event handlers, but I believe this is an easy fix. Two main solutions:

    • Change the href to "javascript:void(0)", or
    • Add "return false" to the end of the "onclick" code.


    Do either of those help?
    I'm the web overlord for Graphic Business Systems

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sdleihssirhc View Post
    I don't have a lot of experience with inline event handlers, but I believe this is an easy fix. Two main solutions:

    • Change the href to "javascript:void(0)", or
    • Add "return false" to the end of the "onclick" code.


    Do either of those help?
    Doing the first option is a definate no-no, because the "javascript:" part is only an unused label (it does not specify that you're using javascript) and it won't mean anything when scripting is not available.

    Go with the second option, which ensures that when clicking on the link, the browsers default behaviour doesn't not occur.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Doing the first option is a definate no-no, because the "javascript:" part is only an unused label (it does not specify that you're using javascript) and it won't mean anything when scripting is not available.

    Go with the second option, which ensures that when clicking on the link, the browsers default behaviour doesn't not occur.
    all i can say is ur a genius paul !!!

    Code:
    echo "<a href=\"#\" id='$i' class='pagenum' onclick='ajaxpost_rep_gen($i);change($i);return false;'/>" . $i . "</a>" . "&nbsp;&nbsp;";
    both method worked charm. i stick with the above method as per ur advise.

    Thank you!

  5. #5
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Doing the first option is a definate no-no, because the "javascript:" part is only an unused label (it does not specify that you're using javascript) and it won't mean anything when scripting is not available.
    Can you explain a little bit more what you mean by "it won't mean anything when scripting is not available"? Are we just talking semantics, here? When I tried "javascript:void(0)" in Chrome, Firefox, and IE (all with JS disabled), they didn't try to load a new page; they all seemed to recognize that JS was trying to execute, and prevented it... which prevented any action from being taken.
    I'm the web overlord for Graphic Business Systems

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sdleihssirhc View Post
    Can you explain a little bit more what you mean by "it won't mean anything when scripting is not available"?
    Sure thing.

    If void were doing something useful, it wouldn't carry on to have its effect when you disable javascript. Void is not doing what you have been mislead in to believing what it does.

    Use just "javascript:" as the href property and you'll find that the same lack of behaviour occurs. It has nothing to do with void at all.
    Void just leads to confusion because people have no idea of what is happening, or why. Here's some info too about void, a bad part or javascript.

    It's better to use techniques that make it easier to understand what is going on. Returning false from the onclick event serves that purpose nicely.
    The other reason to use the return false from the onclick event is that it allows you to cleanly separate the scripting from the content.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •