Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Parsing urls with hash symbols

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #163190
    Chris Lowles
    Participant

    Hello CSS-Tricks, Chris here.

    I was just wondering if parsing a url with multiple hashes is possible, I’m trying to create a hash-symbol based system and I was needing to make something like this:

    http://www.example.com/#function1#function2#function3
    

    Different hashes execute different functions and enable some sort of customization is what I’m aiming for, but I am clueless at making this possible.

    While I may be able to do individual and singular hash-symbol based responses, I don’t know how to make an array and having it working like normal.

    Sorry if I was blurry in this post, anyway I made a pen here, for anyone to inspect if you know what I need to do.

    #163327
    Chromawoods
    Participant

    Why do you want to use hashes instead of query string params?

    #163413
    noahgelman
    Participant

    @Chromawoods, not relevant.

    @Chris Lowles, here’s an option:

    // Get the full url
    var url = window.location.href;
    
    // Using the url we create an array of values split by the #. Google 'javascript split' if you don't understand
    var hashes = url.split('#');
    
    // Check if the array is bigger 1 value. If there is no # the array will be 1 in length and it will just be equal to the url
    if(hashes.length > 1) {
    
      // Typical For loop. We start at 1 and not 0 since the array length starts counting at 1 but the array counts positions starting at 0
      for (var i = 1; i < hashes.length; i++) {
    
      // Run the function. We run the # value through the window to grab the function. This is a bit harder to explain so just take my word for it
      window[hashes[i]]();
    
      }
    
    }
    

    I should run even if placed inside a jquery $(document).ready(). Let me know if if doesn’t.

    #163471
    Chromawoods
    Participant

    @noahgelman lol. I was not criticizing his choice – I was just curious. No harm in that, I believe. I haven’t really come across this scenario before so that is why I asked.

    #163509
    nkrisc
    Participant

    @noahgelman It’s a perfect valid question he asked. He didn’t say, “Why do you want to use hashes instead of query string params? That’s stupid” he simply asked a question.

    #163765
    Chris Lowles
    Participant

    Just updated the pen, would this work?

    #163766
    Chris Lowles
    Participant

    Wait scratch that, its working thanks!!!

    #163805
    Taufik Nurrohman
    Participant

    Why don’t you use this format:

    http://www.example.com/#function1&function2&function3
    

    and…

    var fn = window.location.hash.replace('#', "").split('&');
    
    if(fn.length) {
        for (var i = 0; i < fn.length; i++) {
            window[fn[i]]();
        }
    }
    
Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.