Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Posts
    390
    Thanks
    48
    Thanked 1 Time in 1 Post

    Reading Variables From URL String

    Clicking on one of my buttons will change the page URL. My goal is to read the URL and show different contents on load in case someone has sent someone a direct link to a particular section.

    Code:
    var vars;
    function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('=');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    I've tried the following in my script with no apparent outcome, no alerts showing upon testing and whatnot.I need this to function before all the content shows to be able to hide #Fundraiser_Categories if needed and show something different.
    Last edited by MrTIMarshall; 04-23-2014 at 10:53 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Are you referring to the querystring parameters (e.g. ?foo=bar&test=hello) or hash (e.g. #Fundraiser_Categories)?

    It looks like you are referring to hash.

    Code:
    //this returns Fundraiser_Categories (without the hash symbol) if the current url contains #Fundraiser_Categories
    //if there is no hash, returns empty string.
    console.log(location.hash.substring(1));

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Posts
    390
    Thanks
    48
    Thanked 1 Time in 1 Post
    Quote Originally Posted by glenngv View Post
    Are you referring to the querystring parameters (e.g. ?foo=bar&test=hello) or hash (e.g. #Fundraiser_Categories)?

    It looks like you are referring to hash.

    Code:
    //this returns Fundraiser_Categories (without the hash symbol) if the current url contains #Fundraiser_Categories
    //if there is no hash, returns empty string.
    console.log(location.hash.substring(1));
    I believe I have it working as long as I can remove the # upon clicking a one of my divs, I don't want the # to be added to the URL. To see the result, check this which is the pushed URL without the has being added.

    Untitled Document

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Posts
    390
    Thanks
    48
    Thanked 1 Time in 1 Post
    Bingo!

    I've resolved using: history.pushState(null, null, "?Category=fundraising&content=" + DomainPush);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •