使ç¨jsè·åQueryStringçæ¹æ³å°ç»_javascriptæå·§
程序员文章站
2022-05-04 17:52:49
...
æ¬æåçæ¯ä½¿ç??æ£å??¡????¾¾å¼å¹??
location.searchä¸çå符串ãå
¶ä¸ä¸ä¸ªä¸»??¦å½æ??为 getQueryString()ãgetQueryStringByName(name)ågetQueryStringByIndex(index)
//??·åQueryStringçæ??ç»
function getQueryString(){
var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+","g"));
for(var i = 0; i result[i] = result[i].substring(1);
}
return result;
}
//æ ¹æ®QueryStringåæ??åç???㨨·åå¼
function getQueryStringByName(name){
var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
if(result == null || result.length return "";
}
return result[1];
}
//æ ¹æ®QueryStringåæ??ç´¢å¼??·åå¼
function getQueryStringByIndex(index){
if(index == null){
return "";
}
var queryStringList = getQueryString();
if (index >= queryStringList.length){
return "";
}
var result = queryStringList[index];
var startIndex = result.indexOf("=") + 1;
result = result.substring(startIndex);
return result;
}
æµ??¯??¡µ??¢??·¯å¾ï¼QueryStringDemo.html?id=5&type=1&flag=0
??¡µ??¢å ??½½æ¶ï¼
å??QueryString's nameåçææ¬æ¡ä¸??¾å ¥??¦??·åçQueryStringçåç???㨨·åç¸åºçå¼ï¼
å??QueryString's indexåçææ¬æ¡ä¸??¾å ¥??¦??·åçQueryStringçç´¢å¼??·åç¸åºçå¼ï¼ç´¢å¼ä»0å¼å??ï¼ï¼
??¿æ ·å????å¯ä»¥å????¡µ??¢ä¸æ¹ä¾¿ç??·åQueryStringçå¼äºãæå??ä¸æµ??¯??¡µ??¢QueryStringDemo.htmlçæºä»£ç ï¼
QueryString??·åæ¼ç??ºä»£ç www.jb51.net
QueryString :
QueryString's name :
QueryString's index :
ç»æ ï¼
pdfçä¸??½½å??å
ä¸ä¸ªä¸»??¦æ¹æ³ï¼
æ¹æ³ |
??¯´æ |
getQueryString |
??·åQueryStringçæ??ç»ã ä¾å¦??·¯å¾QueryStringDemo.html?id=5&type=1&flag=0 ????ç??å??¿å["id=5", "type=1", "flag=0"] |
getQueryStringByName |
æ ¹æ®QueryStringåæ??åç???㨨·åå¼ |
getQueryStringByIndex |
æ ¹æ®QueryStringåæ??ç´¢å¼??·åå¼ |
å??å¶ä»£ç 代ç å¦ä¸:
//??·åQueryStringçæ??ç»
function getQueryString(){
var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+","g"));
for(var i = 0; i result[i] = result[i].substring(1);
}
return result;
}
//æ ¹æ®QueryStringåæ??åç???㨨·åå¼
function getQueryStringByName(name){
var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
if(result == null || result.length return "";
}
return result[1];
}
//æ ¹æ®QueryStringåæ??ç´¢å¼??·åå¼
function getQueryStringByIndex(index){
if(index == null){
return "";
}
var queryStringList = getQueryString();
if (index >= queryStringList.length){
return "";
}
var result = queryStringList[index];
var startIndex = result.indexOf("=") + 1;
result = result.substring(startIndex);
return result;
}
æµ??¯??¡µ??¢??·¯å¾ï¼QueryStringDemo.html?id=5&type=1&flag=0
??¡µ??¢å ??½½æ¶ï¼
å??QueryString's nameåçææ¬æ¡ä¸??¾å ¥??¦??·åçQueryStringçåç???㨨·åç¸åºçå¼ï¼
å??QueryString's indexåçææ¬æ¡ä¸??¾å ¥??¦??·åçQueryStringçç´¢å¼??·åç¸åºçå¼ï¼ç´¢å¼ä»0å¼å??ï¼ï¼
??¿æ ·å????å¯ä»¥å????¡µ??¢ä¸æ¹ä¾¿ç??·åQueryStringçå¼äºãæå??ä¸æµ??¯??¡µ??¢QueryStringDemo.htmlçæºä»£ç ï¼
å??å¶ä»£ç 代ç å¦ä¸:
QueryString :
QueryString's name :
QueryString's index :
ç»æ ï¼
pdfçä¸??½½å??å