Js+Jq获取URL参数的集中方法
来源:互联网 发布:手机淘宝搜索排名规则 编辑:程序博客网 时间:2024/05/01 13:28
JQ取值方法:
jquery本身也不存在取得URL参数的方法,但是已经存在插件,可以直接取得URL等参数
插件连接主页:https://github.com/allmarkedup/jQuery-URL-Parser
下载链接:http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip
Examples of use
Using the current page's url (for these examples https://mysite.com/information/about/index.html?itemID=2&user=dave):
// get the protocol
jQuery.url.attr("protocol") // returns 'http'
// get the path
jQuery.url.attr("path") // returns '/information/about/index.html'
// get the host
jQuery.url.attr("host") // returns 'mysite.com'
// get the value for the itemID query parameter
jQuery.url.param("itemID") // returns 2
// get the second segment from the url path
jQuery.url.segment(2) // returns 'about'
Using a different url to the current page:
// set a different URL and return the anchor string
jQuery.url.setUrl("http://allmarkedup.com/category/javascript/#footer").attr("anchor") // returns 'footer'
JS原生获取:
最原始的JS方法:
var URLParams = new Array();var aParams = document.location.search.substr(1).split('&');for (i=0; i < aParams.length ; i++){ var aParam = aParams[i].split('='); URLParams[aParam[0]] = aParam[1];}
这样调用:
http://127.0.0.1/index.php?name=name1&cid=123
//取得传过来的name参数
name=URLParams["name"];
document.write(name);
//取得传过来的cid
cid=URLParams["cid"];
正则分析法:方法一:
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
这样调用:
alert(GetQueryString("参数名1"));
alert(GetQueryString("参数名2"));
alert(GetQueryString("参数名3"));
方法二:
<span style=
"font-size: 16px;"
><Script language=
"javascript"
>
function
GetRequest() {
var
url = location.search;
//获取url中"?"符后的字串
var
theRequest =
new
Object();
if
(url.indexOf(
"?"
) != -1) {
var
str = url.substr(1);
strs = str.split(
"&"
);
for
(
var
i = 0; i < strs.length; i ++) {
theRequest[strs[i].split(
"="
)[0]]=unescape(strs[i].split(
"="
)[1]);
}
}
return
theRequest;
}
</Script>
这样调用:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
</Script>
- Js+Jq获取URL参数的集中方法
- Js+Jq获取URL参数的集中方法示例代码
- 一个JS获取url参数的方法
- js获取url参数的方法
- js获取URL参数的方法
- js获取url参数的简易方法
- js 获取url参数的方法
- js获取url参数的方法
- Js获取Url里参数的方法
- Js 获取url参数的方法
- js获取Url参数的一般方法
- js中获取URL参数的方法
- js获取url中参数的方法
- 用JS获取url参数的方法
- js 获取url参数方法
- JS获取URL参数方法
- js获取url参数方法
- js获取url参数方法
- <菜鸟学算法-A排序(分治的思想:堆排序)>
- PHP 字符串长度计算 - strlen() 函数
- 18、从头学Android之BroadcastReceiver
- 那时候母亲的电话打得很勤
- linux no space left on device的解决办法(iNode满导致)
- Js+Jq获取URL参数的集中方法
- str vs repr
- Fedora 20 音视频播放
- Xcode从5.1升级到5.1.1后,重装dyci
- 早教B2C商城SEO分析之收录数据-转自ITSEO老狼
- 无聊写了个抓网页js中链接,当做个笔记
- 17、从头学Android之Service初步二
- 苹果App Store审核指南中文翻译(更新)
- hibernate4按照帮助文档配置了好久没有成功的问题