jquery 获取URL参数并转码,首先构造一个含有目标参数的正则表达式对象,匹配目标参数并返回参数值
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<script type= "text/javascript" >
$(document).ready( function (){
var pic_url=getUrlParam( "picture" );
$( "#childpic" ).attr( "src" ,pic_url);
var content=getUrlParam( "content" );
$( "#content" ).html( "<b>" +content+ "</b>" );
});
function getUrlParam(name){
//构造一个含有目标参数的正则表达式对象
var reg = new RegExp( "(^|&)" + name + "=([^&]*)(&|$)" );
//匹配目标参数
var r = window.location.search.substr(1).match(reg);
//alert(r);
//返回参数值
if (r!= null ) return decodeURI(r[2]);
return null ;
}
</script>
|
测试url:http://xxxx.html?picture="XXXX"&content="你好!"