Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    javascript back button

    I am doing a javascript back button within my form

    <input type=submit value="Back to previous page" class=header onClick="history.go(-1)">

    for some reason it is submitting the form instead of going back a page --

    why would this be?

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Code:
    onClick="history.go(-1); return false;"
    Perhaps you should consider not using a submit button in the first place.

    Code:
    <input type="button" … onClick="history.go(-1); return false;">
    Bear in mind that this isn't going to work if the user doesn't have js enabled/available.
    Ideally, you'd make the presence of this button dependant on js, either by using js to inset it into the page or by only having this button appear beyond some js-only threshold.


    Alternatively, you could consider using PHP to dynamically code the url into a proper link based on the referrer.

    e.g.
    Code:
    <p><?php if (isset($_SERVER['HTTP_REFERER'])) echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Back</a>'; ?></p>
    Last edited by Bill Posters; 05-28-2006 at 04:18 PM.

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Italy
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try to use: history.back()

  • #4
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    David, the issue wasn't the JavaScript itself, but rather the fact that the input type was submit rather than button. They are quite different in that the form is sent to it's defined or undefined referrer onclick of type=submit.


  •  

    Posting Permissions

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