博客
关于我
页面后退刷新、无刷新
阅读量:160 次
发布时间:2019-02-28

本文共 1232 字,大约阅读时间需要 4 分钟。

情境一:支付金额默认与用户输入不一致问题

页面默认选中100元购买1个礼包,需支付金额为100元。当用户输入其他金额后,上述选项丢失且需支付金额应更新至用户输入值。然而,当用户点击支付后返回时,默认选项恢复,且需支付金额重置为100元,而输入框仍显示用户输入值。这与业务流程不符。

解决方案:通过HTML缓存控制和页面刷新机制

  • HTML头部禁止缓存:
  • 此方法在手机端无效,因部分浏览器忽略缓存控制。

    1. 强制刷新界面:
    2. if(window.name != "bencalie"){location.reload();window.name = "bencalie";}else{window.name = "";}
      1. 页面加载事件处理:
      2. window.onload = function() {    var user_input = +$("#user_input_money").val();    if(user_input > 0) {        // 处理逻辑:恢复默认选项并更新需支付金额        $(".active").removeClass("active");        $(".need-money").text(user_input);    }}
        1. PHP处理:
        2. header("Cache-Control:no-store,no-cache,must-revalidate");header("Cache-Control:post-check=0,pre-check=0", false);header("Pragma:no-cache");

          情境二:页面跳转前状态保留问题

          页面标题列表展开内容默认展开第一个标题。当用户点击非默认标题查看文章详情时,需保留展开的状态。解决方案:利用History API(pushState、replaceState)和popstate事件。

          实现方法:

        3. 在点击标题时调用:
        4. window.history.pushState({html: content, pageTitle: title, id: id}, "", linkUrl);
          1. 列表返回时触发popstate事件,恢复状态。
          2. 注意事项:

            • pushState和replaceState仅适用于同域链接
            • popstate事件可获取状态信息
            • window.onpopstate事件处理状态变化

            代码示例:

            window.history.replaceState({    html: datahtml,    pageTitle: titlehtml,    id: id}, "", lurl);

            技术小知识:

            • 浏览器支持History API
            • hashchange事件处理链接变化
            • popstate事件处理页面跳转
            • onbeforeunload和onunload事件处理页面退出

            以上方法可有效解决页面状态保留问题。

    转载地址:http://sqhc.baihongyu.com/

    你可能感兴趣的文章
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>
    MYSQL CONCAT函数
    查看>>
    multiprocessing.Pool:map_async 和 imap 有什么区别?
    查看>>