Home  >  Article  >  Web Front-end  >  Get URL parameters and transcode them through jquery_jquery

Get URL parameters and transcode them through jquery_jquery

WBOY
WBOYOriginal
2016-05-16 16:39:251697browse

jquery obtains URL parameters and transcodes them. First, it constructs a regular expression object containing the target parameters, matches the target parameters and returns the parameter value

The code is as follows:

<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>

Test url: http://xxxx.html?picture="XXXX"&content="Hello!"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn