从QueryString和Hash中读取值
Reading values both from QueryString and Hash?
有很多js代码来读取查询字符串。
然而,在我看到facebook login
的回复后,这是类似
http://localhost:55643/WebSite2/HTMLPage2.htm#access_token=CAACSIC6Koe......roHXCK8ZD&expires_in=5439
我对自己说,我必须写一些代码来处理哈希(#
)之后的值。
于是我照做了:
(function ($)
{
$.getQs = function (specificUrl)
{
var st = specificUrl || window.location.href;
var o = {}, e;
var re = /([^#?&=]+)=([^&#]*)/ig;
while (e = re.exec(st))
{
o[e[1]] = e[2];
}
//console.log(o);
return o;
}
})(jQuery);
这将返回 对象 在QS
和hash
(如果没有定义specifiedUrl
-它将查看浏览器的url)
用法1 : (特定 URL):
console.log($.getQs('www.example.com?ferko=suska&ee=huu#a=1&b=2&c=3'));
这将返回
Object {ferko: "suska", ee: "huu", a: "1", b: "2", c: "3"}
用法2: (当前 URL):
我当前的URL:
http://localhost:55643/WebSite2/HTMLPage.htm?ferko=suska&ee=huu#a=1&b=2&c=3
so $.getQs()
也收益率
Object {ferko: "suska", ee: "huu", a: "1", b: "2", c: "3"}
那么问题在哪里呢?
在这里:
http://localhost:55643/WebSite2/HTMLPage.htm?ferko=suska&ee=huu#a=1&b=2&c=3&ee=grrr
注意QS侧有ee
, hash
侧也有ee
。
我如何在我的对象中反映它?
<标题>编辑这就是我如何读取facebook的期望值
console.log($.getQs('http://localhost:55643/WebSite2/HTMLPage2.htm#access_token=CAACSIC6KoeroHXCK8ZD&expires_in=5439').access_token);
CAACSIC6KoeroHXCK8ZD
(function ($) {
$.getQs = function (specificUrl) {
function parseToObj(str, re) {
var o = {};
while(e = re.exec(str))
o[e[1]] = e[2];
return o;
}
var st = specificUrl || window.location.href;
return {
beforeHash: parseToObj(st, /([^#?&=]+)=([^&#]*)(?=.*?'#)/ig),
afterHash: parseToObj(st, /([^#?&=]+)=([^&#]*)(?!.*?'#)/ig)
};
}
})(jQuery);
或更好的解决方案:
(function ($) {
$.getQs = function (specificUrl) {
function parseToObj(str, re) {
var o = {};
while(e = re.exec(str))
o[e[1]] = e[2];
return o;
}
var st = specificUrl || window.location.href;
var hashPos = st.indexOf('#');
if(hashPos == -1) hashPos = st.length;
return {
beforeHash: parseToObj(st.substring(0, hashPos), /([^#?&=]+)=([^&#]*)/ig),
afterHash: parseToObj(st.substring(hashPos), /([^#?&=]+)=([^&#]*)/ig)
};
}
})(jQuery);
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 如何在Javascript中读取unsigned int
- 如何读取表单中的输入数据
- 通过传递图像的URL通过javascript读取/处理图像-类似于PHP中的file_get_contents
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- angularjs无法读取未定义的属性then
- 从QueryString和Hash中读取值
- 如何读取当前页面URL的#hash(或片段标识符)