Home > Article > Web Front-end > Example tutorial of h5History mode
I recently saw the implementation of HTML5 History mode routing in vue-router, and then I went to study HTML5 History. Here are some of my understandings. By the way, I used jquery to write an implementation similar to the HTML5 History mode router in vue-router. , in order to achieve the purpose of practicing and becoming familiar with it.
1. history.pushState
history.pushState(state, title, url);
The first and second parameters above can be empty, mainly the third parameter, which means The address of the new historical record, the browser will not load the URL after calling the pushState() method. The new URL does not have to be an absolute address. If It is relative to , it must be relative to the current URL
## 2. history.replaceState
history.replaceState(state, title, url);
window.history.replaceState is similar to
window.history.pushState, except that
replaceState
Won't Add a new history record point in window.history
, the effect is similar to window.location.replace(url), both are not A new recording point will be added to the historical recording point.
window.onpopstateCome
url changes
window.addEventListener("popstate", currentState =
window.history.pushState and window.history.replaceState
will not
triggeronpopstate Event, click forward or backward in browser
will trigger Google Chrome and Firefox have different reactions when the page is first opened. Google Chrome strangely responds Triggers the
event, but Firefox does not 4. Below is an example of an HTML5 mode similar to vue-router, purely to deepen understanding, and the writing is very rough.
76c82f278ac045591c9159d381de2c579fd01892b579bba0c343404bcccd70fb93f0f5c25f18dab9d176bd4f6de5d30ea80eb7cbb6fff8b0ff70bae37074b813b2386ffb911b14667cb8f0f91ea547a7HTML5 History 模式(第二版)6e916e0f7d1e588d4f442bf645aedb2f0e2747f14b81cd86c404d16c6ead21a346d5fe1c7617e3914f214aaf043f4ccf.container-bg{width:1000px; overflow: hidden; margin-right: 0 auto;}.pagination{width: 1000px; background-color: #d8d8d8; height: 30px; line-height: 30px;}.pagination li{width: 100px; height: 30px; background: red; float: left; cursor: pointer; color:#fff; margin: 0 10px 0 0;}531ac245ce3e4fe3d50054a55f2659279c3bca370b5104690d9ef395f2c5f8d16c04bd5ca3fcae76e30b72ad730ca86db0059ac5cf5908bb26cca21bb12cfd28051ed3719fd88644fafce0a58161256625edfb22a4f469ecb59f1190150159c61bed06894275b65c1ab86501b08a632eb25edfb22a4f469ecb59f1190150159c62bed06894275b65c1ab86501b08a632eb25edfb22a4f469ecb59f1190150159c63bed06894275b65c1ab86501b08a632eb929d1f5ca49e04fdcb27f9465b944689b3b8810c81dca5c67dae79332813eebf929d1f5ca49e04fdcb27f9465b94468916b28748ea4df4d9c2150843fecfba684fd2989e8d240aee500d7f8197e051e42cacc6d41bbb37262a98f745aa00fbf08019067d09615e43c7904885b5246f0a history.replaceState(null, "页面标题", "http://127.0.0.1:3000/lmw/0");//当页面载入时候,把url地址修改var searchObject = {};/*此对象用来保存下面pushState的URL作为key值,ajax要查询的ID为val *例如:searchObject = {"http://127.0.0.1:3000/lmw/0":0}*/var factory = function(){var addva = document.location.href;//获取完整URLvar query = searchObject[addva];//找到该URL对应的值 query = (query == undefined ? 0 :query);//发起ajax加载页面 $.get("/page?page="+query,function(data){var data2 = JSON.parse(data);var ele = ""for(var i=0;i<data2.data.length;i++){ ele += '25edfb22a4f469ecb59f1190150159c6'+data2.data[i].name+'bed06894275b65c1ab86501b08a632eb'} $('.ptting').html(ele) }) }; //点击分页切换事件 $(".pagination li").click(function(){var query=$(this).index(); $.get("/page?page="+query,function(data){var data2 = JSON.parse(data);var ele = ""for(var i=0;i<data2.data.length;i++){ ele += '25edfb22a4f469ecb59f1190150159c6'+data2.data[i].name+'bed06894275b65c1ab86501b08a632eb'} $('.ptting').html(ele) history.pushState({pageIndex : 1}, "", "http://127.0.0.1:3000/lmw/"+query);//把当前pushState的url,和ajax查询的值存入对象,以供触发pushState事件的时候使用 searchObject["http://127.0.0.1:3000/lmw/"+query] = query }) })//浏览器前进或者后退的时候触发popstate事件if (history.pushState) { window.addEventListener("popstate", function() { factory() }); factory() };2cacc6d41bbb37262a98f745aa00fbf036cc49f0c466276486e50c850b7e495673a6ac4ed44ffec12cee46588e518a5e
var fs = require('fs')var path = require('path')var express = require('express')var app = express(); app.use(express.static('./public'));var router = express.Router(); router.get('/page',function(req,res){var page = req.query.pagetry{var text = fs.readFileSync('./data'+page+'.json'); res.json(text.toString()) }catch(err){ res.send('哈哈!傻逼,没有拉!') } }) app.use(router) app.listen(3000)
注意:history.pushState({pageIndex : 1}, "", "http://127.0.0.1:3000/lmw/"+query)这里第三个参数写了完整的绝对路径,如果写成"/lmw/"+query这样的相对路径,会随着query值得增加无限在url后面追加,因为相对路径它一定是相对于当前的URL
The above is the detailed content of Example tutorial of h5History mode. For more information, please follow other related articles on the PHP Chinese website!