SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: "Go Back" code?

  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question "Go Back" code?

    Hi everybody!

    I wondering if there is any kind of code (JavaScript, PHP, whatever) that can make someone "go back" to the page they just came from? I'm not that sharp on JavaScript, and since I have a global-kind of navigation to where the users may find themselves being on page 2 going to page B when they would like to go back to page 2 when the "back button" is clicked on... Only problem is that I have the back link setup to send them back to the index page right now via standard hyperlink markup (which was the only way I knew how to fix this).

    Any JS that can handle this for me? The "back button" is on a php login page... Thanks everyone.

  2. #2
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try a link like this
    <a href="javascript:history.back(1)" >&lt;- Back</a>

  3. #3
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    <a href="#" onclick="history.go(-1);return false">go back</a>

    [Edit] too late

  4. #4
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet. Thanks guys.

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how can you do a forward link once some one has gone back?

  6. #6
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try history.forward()

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You can either use history.back() and history.forward() to go back and forward one page or use history.go(n) where n is the number of pages to go forward (and can bbe negative to go back).

    Also there is no need for an <a> tag as the onclick event handler to process it can be attached to any tag in the page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys

  9. #9
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you using JS? ... You should use PHP.

    Code:
    <a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Go Back</a>
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  10. #10
    SitePoint Enthusiast kceleb9's Avatar
    Join Date
    Jun 2008
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need this to be triggered in response to a user action? Or an automatic event?

    If user action then the history.go(-1) is the preferred solution. history.back() still breaks in some browsers. These are not perfectly reliable though -- as in case where a user comes to your page by shift-clicking a link - the resulting window object will have and empty history array - hence the JS wont work.
    - PM me to start building fair link exchanges automatically -

    LnkPro.com - Link Exchanges for Professionals


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
  •