jquery获取URL参数

来源:互联网 发布:犀牛软件安装失败 编辑:程序博客网 时间:2024/06/02 07:28

本文转自:http://blog.csdn.net/salc3k/article/details/7286296

代码示例:

1.html:

<!DOCTYPE html><html> <head> <title></title> <style type='text/css'></style></head> <body></body> <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script><script type='text/javascript' src='jqueryPlugin.js'></script><script type='text/javascript'> $(document).ready(function(){// 访问该页面时,在url末尾添加?picid=8console.log(window.location.href);console.log($.getUrlParam('picid'));console.log(getUrlParam('picid'));});function getUrlParam(name){//构造一个含有目标参数的正则表达式对象var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");//匹配目标参数var r = window.location.search.substr(1).match(reg);//返回参数值if (r!=null) return unescape(r[2]);return null;}</script> </html>

 

jqueryPlugin.js:

<!DOCTYPE html><html> <head> <title></title> <style type='text/css'></style></head> <body></body> <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script><script type='text/javascript' src='jqueryPlugin.js'></script><script type='text/javascript'> $(document).ready(function(){// 访问该页面时,在url末尾添加?picid=8console.log(window.location.href);console.log($.getUrlParam('picid'));console.log(getUrlParam('picid'));});function getUrlParam(name){//构造一个含有目标参数的正则表达式对象var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");//匹配目标参数var r = window.location.search.substr(1).match(reg);//返回参数值if (r!=null) return unescape(r[2]);return null;}</script> </html>


运行结果:file:///C:/Users/zhanglei/Desktop/1.html?picid=8
8

8


1 0
原创粉丝点击