javascript - How to use multiple hashes? - Stack Overflow

Currently I use page numbers in hashes with Ben's Alman jquery-hashchange plugin:$(document).ready

Currently I use page numbers in hashes with Ben's Alman jquery-hashchange plugin:

$(document).ready(function(){
    $(window).hashchange( function(){
      var hash = (location.hash) ? location.hash.slice(1) : 'page1';
      $.ajax({
          url: '/list/' + hash, // result url like page1, page2 etc.

Now I need to add there one more value - filter. I think result hash URL can look like

#page1&filter=1-1-0 
#filter=1-1-0 (if page number is omitted)
#page1 (if filter is not defined)

How to parse that? I.e. how to understand if page is defined, if filter is defined (and what are the values - 1, 1 and 0 - I need them separately)?

I was thinking about Ben's Alman BBQ plugin, but (1) it looks too plicated for such simple task, (2) not sure how to use parameters (page1, page2 etc.) without values.

Currently I use page numbers in hashes with Ben's Alman jquery-hashchange plugin:

$(document).ready(function(){
    $(window).hashchange( function(){
      var hash = (location.hash) ? location.hash.slice(1) : 'page1';
      $.ajax({
          url: '/list/' + hash, // result url like page1, page2 etc.

Now I need to add there one more value - filter. I think result hash URL can look like

#page1&filter=1-1-0 
#filter=1-1-0 (if page number is omitted)
#page1 (if filter is not defined)

How to parse that? I.e. how to understand if page is defined, if filter is defined (and what are the values - 1, 1 and 0 - I need them separately)?

I was thinking about Ben's Alman BBQ plugin, but (1) it looks too plicated for such simple task, (2) not sure how to use parameters (page1, page2 etc.) without values.

Share Improve this question edited Feb 13, 2013 at 9:14 James Allardice 166k22 gold badges334 silver badges315 bronze badges asked Nov 5, 2011 at 8:54 LA_LA_ 20.4k60 gold badges179 silver badges318 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Very simple and unextediable parser for two hardcoded variables:

var hash_parts = location.hash.split('&', 2); //2 - limit, may be changed if more than two arguments

for(i in hash_parts) {
    if(hash_parts[i].indexOf("page") === 0) { //begins with "page"
        var current_page_number = hash_parts[i].substr(4);
    }
    else if(hash_parts[i].indexOf("filter") === 0) { //begins with "filter"
        var filter = hash_parts[i].split('=', 2);
        var filer_values = filter[1].split('-'); //filter_values == {'1', '1', '0'}
    }
}

You can easily make it universal.

Please, also take a look here: Parse query string in JavaScript - just change window.location.search.substring(1) to hash.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744683365a4587767.html

相关推荐

  • javascript - How to use multiple hashes? - Stack Overflow

    Currently I use page numbers in hashes with Ben's Alman jquery-hashchange plugin:$(document).ready

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信