HTML5网页页面无刷新更新页面URL

HTML5可通过history.pushState修改网页的URL地址,在配合相关代码显示隐藏相应界面便可以实现单页面多界面相互操作。该方法比直接访问URL地址速度快,执行效率高,UI体验好,但会增加页面的复杂性及耦合性,要视实际情况而定,一般都用在dialog弹出框上。
实例:
这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <title>HTML5刷新</title>
    <meta name="full-screen" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="margin: 0;padding: 0;">
    <button>弹出框</button>
    <div class="dialog" style="width: 100%;height: 100%;position: absolute;background-color: red;display: none;top:0;">新页面</div>
</body>
<script src="http://www.zeptojs.cn/zepto.min.js"></script>
<script type="text/javascript">

$(function(){
    $("button").click(function(){
        //html5 无刷新修改URL pushState 重复地址会添加到历史页面 replaceState 重复地址不会添加到历史页面
        history.pushState("","",window.location.href+"#dialog");
        $(".dialog").show();
    })

    //刷新监听
    window.onload = function(){
        if(window.location.href.indexOf("dialog") != -1)
            history.go(-1);
    }

    //浏览器回退监听
    window.onpopstate = function () {
        if($(".dialog").css("display")!="none")
            $(".dialog").hide();
    }
})
</script>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值