通用JS工具类封装(获取服务器端口url、参数功能)
来自CloudWiki
获取服务器地址
在工具类mm.js中添加如下代码
以获取服务器地址:
var conf = { serverHost :'' } //获取服务器地址 getServerUrl: function(path){ return conf.serverHost + path; }
获取URL参数
在工具类mm.js中添加如下代码:
//获取url参数 getUrlParm:function(name){ //happymmall.com/procuct/list?keyword=***&page=1 var reg = new RegExp('(^|&' + name + '=([^&]*)(&|$)');//参数的正则表达式 var result = window.location.search.substr(1).match(reg);//提取网址中的参数 return result ? decodeURIComponent(result[2] ): null;//返回 },
在index.js中添加:
console.log(_mm.getUrlParam('test'));
在浏览器器上测试: