PHP怎样实现点击浏览器的返回按钮回到页面时刷新该页面?

WBOY
Libérer: 2023-03-02 07:32:01
original
2501 Les gens l'ont consulté

在网上搜的都是ASP的 PHP怎么阻止后退按钮的缓存呢?

回复内容:

在网上搜的都是ASP的 PHP怎么阻止后退按钮的缓存呢?

  1. 无法阻止浏览的history.back()等行为

  2. 可以考虑设置页面为无缓存

    <code>$nocache = array(
        'Expires' => '0',
        'Cache-Control' => 'no-store,private, post-check=0, pre-check=0, max-age=0',
        'Pragma' => 'no-cache'
    );
    foreach($nocache as k => $v)
        header($k.': '.$v);
    </code>
    Copier après la connexion
  3. 如果真的要拒绝后退,可以使用以下方法模拟,考虑做一道跳转,这样后退一次,会自动跳到当前页面,近似于刷新

    <code>header('Location: index.php');</code>
    Copier après la connexion

拿首页分页链接来说:
<a href="/index.php?page=3" onclick="page(3);return false;">3</a>
搜索引擎的爬虫会根据href访问/index.php?page=3获取第3页数据,利于SEO.
用户在浏览器里右键选择"在新标签页中打开"也能正常访问到/index.php?page=3.
如果用户在页面直接点击链接,则触发click事件,由JS通过AJAX加载并渲染局部数据,以及设置location.hash为/index.php#/page/3.
location.hash = "#/page/3";
浏览器能够自行记住location.hash历史记录,我们只需监听location.hash改变的事件hashchange(支持IE8,不支持IE7/6)就能实现用户点击浏览器返回按钮时重新加载页面的效果.

<code>$(window).on("hashchange", function(){
    alert(location.hash); //输出#/page/2
    var arr = location.hash.split("/"); // ["#", "page", "2"]
    if(arr[1] == "page") {
        page(arr[2]); //AJAX局部加载第2页数据
        //location.href = "/index.php?page="+arr[2]+"&"+new Date().getTime(); //直接访问第2页
    }
});</code>
Copier après la connexion

上面这种"SEO和体验并重的超链接设计"应该能满足楼主需求,不过需要一定的改造成本.

页面加载完成js刷新页面location.href = location.href

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!