Scroller dans la page après un submit de formulaire (voir le formulaire plus bas)
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
<?php
/**
 * @page scroller_page_apres_submit.php
 * @brief Ce script remet le scroll de la page là ou il était, après un submit de formulaire.
 *
 * @author hughes monget
 * @date 2012-07-10
 */
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL | E_NOTICE | E_STRICT);
set_time_limit(0);
$arr_ini_set = array('log_errors' => 0, 'display_errors' => 1, 'error_log' => 0, 'html_errors' => 0, 'date.timezone' => 'Europe/Paris');
array_walk($arr_ini_set, create_function('$v, $k', 'ini_set($k, $v);'));
if (!setlocale(LC_ALL, 'french')) { echo 'locale not set'; }
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Scroller dans la page après un submit de formulaire</title>
        <style type="text/css">
        <!--
        body
        {
            overflow: scroll;
        }
        p
        {
            margin: 0;
        }
        -->
        </style>
        <script type="text/javascript">
        <!--;
            /// Fonction qui permet de récupérer la position du scroll de la page.
            /// @see http://www.quirksmode.org/js/doctypes.html
            function get_scroll_top(obj_window)
            {
                var int_top = 0;
                if (obj_window.document.documentElement && obj_window.document.documentElement.scrollTop)
                    { int_top = obj_window.document.documentElement.scrollTop; } // IE6 avec Doctype
                else if (obj_window.document.body)
                    { int_top = obj_window.document.body.scrollTop; } // IE6 sans Doctype
                else if (obj_window.pageYOffset)
                    { int_top = obj_window.pageYOffset; } // NS4, NS6
                return int_top;
            }
            /// Sur le submit du formulaire.
            function on_submit(obj_form)
            {
                var int_hauteur_scroll = get_scroll_top(self);
                // On ajoute au formulaire un champ caché qui contient la hauteur de scroll.
                if (int_hauteur_scroll > 0)
                {
                    var obj_input = document.createElement("input")
                    obj_input.setAttribute("type", "hidden")
                    obj_input.setAttribute("name", "scroll")
                    obj_input.setAttribute("value", int_hauteur_scroll)
                    obj_form.appendChild(obj_input)
                }
            }
            <?php
            /// Si on a reçu la hauteur de scroll en paramètres http,
            /// on force la position du scroll de la fenêtre.
            if (isset($_REQUEST['scroll']) && is_scalar($_REQUEST['scroll']) && ($int_scroll = intval(trim($_REQUEST['scroll']))) > 0)
            {
                echo
                    '
                    window.onload = function()
                    {
                        if (window.scrollTo)
                        {
                            window.scrollTo(0, '.htmlspecialchars($int_scroll).');
                        }
                    }
                    ';
            }
            ?>
        //-->
        </script>
    </head>
    <body>
        <p>Scroller dans la page après un submit de formulaire (voir le formulaire plus bas)</p>
<?php
echo str_repeat('<p>*</p>', 100);
echo '<hr />';
if (isset($_REQUEST['submited']) && is_scalar($_REQUEST['submited']))
{
    // L'utilisateur a submité le formulaire.
    echo 'Submitted-'.number_format(microtime(1), 6).'-'.htmlspecialchars($_REQUEST['submited']);
}
?>
        <table>
            <tr>
                <td>
                    <form action="<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>" onsubmit="on_submit(this)">
                        <input type="hidden" name="submited" value="<?php echo md5(uniqid(mt_rand(), 1)); ?>" />
                        <input type="submit" value="Go" />
                    </form>
                </td>
                <td>
                    <form action="<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>" onsubmit="on_submit(this)">
                        <input type="submit" value="Reset" />
                    </form>
                </td>
            </tr>
        </table>
        <hr />
<?php
echo str_repeat('<p>*</p>', 100);
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>