1
loading 2015-03-28 11:42:20 +08:00 via Android
这应该发 问与答,创造问题不属于创造!
|
2
iugo OP @loading 刚才我想了想, 没有作为类, 而是先写成了函数. JavaScript 怎么写类不明白. 不过功能已经实现, 可以正常用.
我的写法和网上搜索到的不一样, 希望从另外一个角度给大家带来点什么东西. |
3
zrp1994 2015-03-28 12:04:46 +08:00 1
@iugo 草草写了个,你感受下
var parseQuery = function(string) { var urlSplit = string.split('?'), querySplit = [], queries = {}; if(urlSplit.length < 2 || !urlSplit[1]) return null; querySplit = urlSplit[1].split('&'); for (var i = 0; i < querySplit.length; i++) { var query = querySplit[i].split('='); if(query.length > 0 && query[0]) queries[query[0]] = query[1]; }; return queries; } console.log(parseQuery('https://www.baidu.com/s?ie=UTF-8&wd=%E6%B5%8B%E8%AF%95')); console.log(parseQuery('https://www.baidu.com/s?ie=UTF-8&=%E6%B5%8B%E8%AF%95')); console.log(parseQuery('https://www.baidu.com/s?ie=UTF-8&wd=')); console.log(parseQuery('https://www.baidu.com/s?ie=UTF-8&=')); console.log(parseQuery('https://www.baidu.com/s?ie=UTF-8')); console.log(parseQuery('https://www.baidu.com/s?')); console.log(parseQuery('https://www.baidu.com/s')); 测试结果: { ie: 'UTF-8', wd: '%E6%B5%8B%E8%AF%95' } { ie: 'UTF-8' } { ie: 'UTF-8', wd: '' } { ie: 'UTF-8' } { ie: 'UTF-8' } null null |
4
ETiV 2015-03-28 12:36:48 +08:00 1
|
5
nowgoo 2015-03-28 13:37:02 +08:00 1
function get_value(name, url) {
return (url.substr(url.indexOf('?')).substr(1).match(new RegExp('(?:^|&)' + name + '=([^&]*)(?:&|$)', 'i')) || [])[1] || ''; } |
6
sumhat 2015-03-28 21:58:40 +08:00 1
楼上的几位,对于有 # 的 URL,你们的代码肯定挂
|