您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JS实现简单路由器功能的方法_javascript技巧

2024/6/17 8:17:42发布19次查看
本文实例讲述了js实现简单路由器功能的方法。分享给大家供大家参考。具体实现方法如下:
var wawa = {};wawa.router = function(){ function router(){ } router.prototype.setup = function(routemap, defaultfunc){ var that = this, rule, func; this.routemap = []; this.defaultfunc = defaultfunc; for (var rule in routemap) { if (!routemap.hasownproperty(rule)) continue; that.routemap.push({ rule: new regexp(rule, 'i'), func: routemap[rule] }); } }; router.prototype.start = function(){ console.log(window.location.hash); var hash = location.hash, route, matchresult; for (var routeindex in this.routemap){ route = this.routemap[routeindex]; matchresult = hash.match(route.rule); if (matchresult){ route.func.apply(window, matchresult.slice(1)); return; } } this.defaultfunc(); }; return router;}();var router = new wawa.router();router.setup({ '#/list/(.*)/(.*)': function(cate, id){ console.log('list', cate, id); }, '#/show/(.*)': function(id){ console.log('show', id); }}, function(){ console.log('default router');});router.start();
希望本文所述对大家的javascript程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product