解析URL#片段的JavaScript [英] Parse URL # fragment with javascript
本文介绍了解析URL#片段的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要分析的哈希代码到一个关联数组在JavaScript中,就像$ _GET超全局变量在PHP。 下面是网址:
I want to parse the hash fragment into an associative array in javascript, just like the $_GET superglobal in PHP. Here is the URL:
www.mysite.com/randompage#name=donald&lastname=mclotsoquestions&age=25
到目前为止,我有这样的:
So far I have this:
var hashfragment = location.hash;
var hashfragment_array = hashfragment.split('&');
Hashfragment_array为[#NAME =唐纳德,姓氏= mclotsoquestions,年龄= 25]
Hashfragment_array is ["#name=donald","lastname=mclotsoquestions","age=25"]
如何创建键值对的呢?
推荐答案
使用 jQuery的烧烤一>,这是一种经过实战检验的库处理状态管理。
Use jQuery BBQ, which is a battle-tested library that handles state management.
$(window).on('hashchange', function(e) { // event fired when the fragment changes
var frag = $.deparam.fragment();
});
这也使得添加或更改片段容易:
It also makes adding or changing the fragment easy:
$.bbq.pushState({ someParam: 'value' });
这篇关于解析URL#片段的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文