此处 url 验证是否合法暂未做,自行根据需求实现更严谨的封装
目前的封装基本上通用,需要注意的是调用的时候url为string类型的,回调返回是的截取完url(obj类型),可直接在回调里面进行使用这些回调;
希望各位的支持,过段时间准备封装一些项目中常用的插件扔到github上。欢迎各位贡献者
// 截取url串 --调用
CutString('http://127.0.0.1:8848/xxxxx/project/home/enterOne.html?type=store&aa=100',function(res){
console.log(res)
})
// url 截串
// 参数: 要揭取的url(string), 回调;
function CutString(URL, callback) {
//TODO-- 验证url 是否合法
console.log(URL)
//判断Url格式是否规范
//var strRegex =
// "^((https|http)://)?[a-z0-9A-Z]{3}\.[a-z0-9A-Z][a-z0-9A-Z]{0,61}?[a-z0-9A-Z]\.com|net|cn|cc (:s[0-9]{1-4})?/$";
//console.log(new RegExp(strRegex).test(URL))
if (typeof(URL) != 'string') {
throw '参数不是 string 类型!'
}
var theRequest = new Object();
if (URL.indexOf("?") != -1) {
var str = URL.split("?")[1].split("&");
for (var i = 0; i < str.length; i++) {
theRequest[str[i].split("=")[0]] = str[i].split("=")[1];
}
}
callback(theRequest);
}