HTML5¼¼Êõ

HTML5 History ģʽ - Ãú¤Î

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-06-24 15:00 ÎÒÒªÆÀÂÛ( )

×î½ü¿´µ½vue-routerµÄHTML5 History ģʽ·ÓɵÄʵÏÖ£¬È»ºó˳±ãÓÖÈ¥Ñо¿ÁËÒ»ÏÂHTML5 µÄ History£¬ÒÔÏÂÊÇ×Ô¼ºµÄһЩÀí½â£¬Ë³±ãÓÃjqueryд Ò»¸öʵÏÖÀàËÆvue-routerÀïÃæHTML5 History ģʽ·ÓÉÆ÷,ÒÔ´ïµ½Á·Á·ÊÖ£¬ÊìϤÊìϤµÄÄ¿µÄ¡£ Ò»¡¢history.pushState history.

×î½ü¿´µ½vue-routerµÄHTML5 History ģʽ·ÓɵÄʵÏÖ£¬È»ºó˳±ãÓÖÈ¥Ñо¿ÁËÒ»ÏÂHTML5 µÄ History£¬ÒÔÏÂÊÇ×Ô¼ºµÄһЩÀí½â£¬Ë³±ãÓÃjqueryд Ò»¸öʵÏÖÀàËÆvue-routerÀïÃæHTML5 History ģʽ·ÓÉÆ÷,ÒÔ´ïµ½Á·Á·ÊÖ£¬ÊìϤÊìϤµÄÄ¿µÄ¡£

Ò»¡¢history.pushState

history.pushState(state, title, url);

 ÉÏÃæµÚÒ»ºÍµÚ¶þ¸ö²ÎÊý¿ÉÒÔΪ¿Õ£¬Ö÷Òª¾ÍÊǵÚÈý¸ö²ÎÊý£¬±íʾÐÂÀúÊ·¼Í¼µÄµØÖ·£¬ä¯ÀÀÆ÷ÔÚµ÷ÓÃpushState()·½·¨ºó²»»áÈ¥¼ÓÔØÕâ¸öURL£¬ÐµÄURL²»Ò»¶¨ÒªÊǾø¶ÔµØÖ·£¬Èç¹ûËüÊÇÏà¶ÔµÄ£¬ËüÒ»¶¨ÊÇÏà¶ÔÓÚµ±Ç°µÄURL

¶þ¡¢history.replaceState

history.replaceState(state, title, url);

window.history.replaceState ºÍ window.history.pushState ÀàËÆ£¬²»Í¬Ö®´¦ÔÚÓÚ replaceState ²»»áÔÚ window.history ÀïÐÂÔöÀúÊ·¼Ç¼µã£¬ÆäЧ¹ûÀàËÆÓÚ window.location.replace(url) £¬¶¼ÊDz»»áÔÚÀúÊ·¼Ç¼µãÀïÐÂÔöÒ»¸ö¼Ç¼µãµÄ¡£

Èý¡¢window.onpopstate

À´¼àÌýurlµÄ±ä»¯

window.addEventListener("popstate", function() { var currentState = history.state; /* * ´¥·¢Ê¼þºóÒªÖ´ÐеijÌÐò */ });

//»òÕß
window.onpopstate = function(){}

javascript½Å±¾Ö´ÐÐ window.history.pushState ºÍ window.history.replaceState ²»»á´¥·¢ onpopstate ʼþ£¬ÔÚä¯ÀÀÆ÷µã»÷Ç°½ø»òÕߺóÍ˻ᴥ·¢

¹È¸èä¯ÀÀÆ÷ºÍ»ðºüä¯ÀÀÆ÷ÔÚÒ³ÃæµÚÒ»´Î´ò¿ªµÄ·´Ó¦ÊDz»Í¬µÄ£¬¹È¸èä¯ÀÀÆ÷Ææ¹ÖµÄÊǻش¥·¢ onpopstate ʼþ£¬¶ø»ðºüä¯ÀÀÆ÷Ôò²»»á

ËÄ¡¢ÏÂÃæÌùÒ»¸öÀàËÆvue-routerµÄHTML5ģʽµÄÀý×Ó£¬´¿Êô¼ÓÉîÀí½â£¬Ð´µÄºÜ´Ö²Ú¡£

HTML5 History ģʽ(µÚ¶þ°æ) .container-bg .pagination .pagination li123 history.replaceState(´Ë¶ÔÏóÓÃÀ´±£´æÏÂÃæpushStateµÄURL×÷ΪkeyÖµ£¬ajaxÒª²éѯµÄIDΪval *ÀýÈ磺searchObject = {"http://127.0.0.1:3000/lmw/0":0}(){ :query); (data){ JSON.parse(data); ){ ele } $().html(ele) }) }; (){ ).index(); $.get((data){ JSON.parse(data); ){ ele } $().html(ele) history.pushState({pageIndex : query); query }) }) (history.pushState) { window.addEventListener(() { factory() }); factory() };

˳±ãÌùÒ»¸önode.jsÖеÄserver´úÂ룬ΪÁ˲âÊÔ£¬ºÜËæÒâ¼òµ¥µÄдÁËÒ»¸ö

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.page try{ 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

·þÎñ¶Ë·ÅÁËdata0.json,data1.json,data2.jsonÀ´Ä£ÄâÒ»ÏÂÊý¾Ý¿âÈ¡Êý¾Ý£¬·þÎñÆ÷¸ü¾ßÇ°¶Ë´«À´µÄindexÖµ£¨0/1/2£©£¬À´Æ¥Åä¶ÁÈ¡data*.jsonÎļþ£¬ÔÙ·¢¸øÇ°¶Ë

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • HTML5-Web SQLÊý¾Ý¿â - СÄϹÏdxn

    HTML5-Web SQLÊý¾Ý¿â - СÄϹÏdxn

    2017-06-21 18:01

  • ´øÄãÁã»ù´¡Ñ§Ï°HTML5 - ʵÑéÂ¥

    ´øÄãÁã»ù´¡Ñ§Ï°HTML5 - ʵÑéÂ¥

    2017-06-20 14:00

  • ΢ÐŹ«ÖÚƽ̨ÍøÒ³¿ª·¢ÊµÕ½--3.ÀûÓÃJSSDKÔÚÍøÒ³ÖлñÈ¡µØÀíλÖã¨HTML5+jQuery£© - Ò»¸ö×ÔÓɵijö°æÈË

    ΢ÐŹ«ÖÚƽ̨ÍøÒ³¿ª·¢ÊµÕ½--3.ÀûÓÃJSSDKÔÚÍøÒ³ÖлñÈ¡µØÀíλÖã¨HTML5

    2017-06-20 10:00

  • HTML5ºÐ×ÓÄ£ÐÍ¡£ - IT-Qcp

    HTML5ºÐ×ÓÄ£ÐÍ¡£ - IT-Qcp

    2017-06-20 09:01

ÍøÓѵãÆÀ
â