SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2012
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Grabbing part of the url

    I want to be able to grab an id (#term) from the end of a url (http://website/page/#term) with Javascript. Not sure how to do that.

    I then want to use it in coordination with the following bit of code and run it just after the page has loaded.

    Code:
    <script>
    $('#filters a').find('.active').removeClass('active');
    $('#filters a').find('#term').addClass('active');
    </script>

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi reedbird,

    You can use window.location.hash to get that part of the URL. Check this page for more information.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2012
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! Appreciate it!

    Now, technically wrong forum here, but how could I get that part of the url with php?

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by reedbird8 View Post
    Now, technically wrong forum here, but how could I get that part of the url with php?
    Unfortunately you can't - that part of the url (the fragment) isn't passed through to the server, it's only available to the browser. If you need that value, you'd have to get it via JS and pass it to your script.

    Edit: If you have a URL as a string (eg. that's been passed to your script as input) you can extract the various parts (including the fragment) using PHP's parse_url function.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2012
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.

    I think using it as javascript is going to work, but its not quite working properly. I'm looking to pull that information and use it with isotope to filter different items on load based on the hash.

    Here is what I have, and its not quite working: (the variable breedername is eventually passed to initialize isotope)

    Code:
    var breeder = window.location.hash;
    var breedername = '';
    	if ( breeder != '' ) {
    		$('#filters a').find('.active').removeClass('active');
    		$('#filters a').find(breeder).addClass('active');
                    var breedern = breeder.slice(1);
    		var breederterm = ".term-";
    		var breedername = breederterm.concat(breedern);
    	}

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Which part of the code isn't working as you expect?

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2012
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's not functioning at all. the variable breedername still ends up empty even if there is a hash. (I know this because it passes to the isotope filter and everything still shows).

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2012
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reedbird8 View Post
    it's not functioning at all. the variable breedername still ends up empty even if there is a hash. (I know this because it passes to the isotope filter and everything still shows).
    I found an error in my html. so It's mostly working now! Thanks!


Bookmarks

Posting Permissions

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