What is the best practice for a "back page" link?

9

Would you like to know the best practice for that default button / link in the applications? Today I use href="javascript:history.back();" but there are some considerations that over time I came across, are 3 doubts :

  • INCOMPATIBILITY - I've been through javascript:history.back(); not being compatible with some browsers / specific versions, and not working, even with JavaScript enabled on client. Is there any other measure to be used for this situation?

  • PHP - Knowing that it is a server-side language, would there be anything in PHP to do this function, native or not, and not use JavaScript? >

  • Note: Regarding doubt 3, I have already used in some systems a kind of page capture by href="javascript:history.back();" , where a history was set up following the navigation, so it could direct a return link to any navigation history level, because sometimes I need to go back one, two, or even 3 pages of this history, I discontinued this function in the new systems because I thought it was unnecessary processing, and I ended up using only session and when necessary on specific occasions I do a "gambiarra".

        
    asked by anonymous 25.10.2015 / 14:35

    1 answer

    12

    Answer 1:

    It has this method, however it is the same as the one you used, and does not work in some browsers.

    <a href="javascript: history.go(-1)">Voltar</a>
    

    This can solve the incompatibility problem:

    <a href="javascript:void(0)" onClick="history.go(-1); return false;">Voltar</a>
    

    Answer 2:

    Just use the same command and change the -1 to -2, and so on respectively.

    <a href="javascript: history.go(-2)">Voltar 2 páginas no histórico</a>
    

    Answer 3:

    I prefer to use sessions just like you said, because it ensures that no incompatibilities will happen. There is the header () function of PHP, but it may happen that it does not work on "HTTPS" protected pages.

    header('Location: '.$_SERVER['HTTP_REFERER']);
    
        
    25.10.2015 / 16:19