10

If I am on https://www.website.com/#something, how can I return the hash value "something" from the URL?

8
  • 4
    window.location.hash its gives u the hash
    – Sandeep
    Commented Apr 1, 2013 at 10:07
  • 1
    @Sandeep: that's clearly the answer, why not simply post it as an answer? Commented Apr 1, 2013 at 10:11
  • @DavidThomas, agreed!
    – user1661548
    Commented Apr 1, 2013 at 10:12
  • posted jus now :) @Christian u can accept it as answer
    – Sandeep
    Commented Apr 1, 2013 at 10:12
  • 1
    Looking at @Sandeep's uppercased question, you may have edited/deleted comments since last I looked, so may I ask: what is it you're trying to do with the hash? Show/hide something, submit it to a server (Ajax, etc), concatenate with another variable or something else entirely? Commented Apr 1, 2013 at 10:24

4 Answers 4

17

window.location.hash its that simple.

donot use all those methods which consume CPU and effects performance.

If DOM provides something predefined use it first.

To pass value to PHP please do and ajax call to php.

var hash = window.location.hash;

$.ajax({
    url: 'someurl.php',
    data: {hash: hash},
    success: function(){}
})
2
  • I knew how to do an ajax call, but that's the only way?
    – user1661548
    Commented Apr 1, 2013 at 10:18
  • ya thats the only way to access a server-side language from browser AFAIK.
    – Sandeep
    Commented Apr 1, 2013 at 11:26
4

You can use the location.hash property to grab the hash of the current page:

var hash = window.location.hash;
1

update

As there is a built in method to get the hash via DOM above answer is not appropriate

   var hashTag = window.location.hash
   alert(hashTag);

will do the magic.

Old answer

You can do something as below if you have multiple hashes in your url

//var href = location.href; // get the url in real worl scenario
var href = "www.bla.com#myhashtag"; // example url
var split = href.split("#"); // split the string; usually there'll be only one # in an url so there'll be only two parts after the splitting
var afterSplit = "Error parsing url";
if(split[1] != null){
    afterSplit = split[1];
}
// If everything went well shows split[1], if not then de default error message is shown
alert(afterSplit);

Here is an example Live Fiddle

4
  • DOM already provides u a predefined variable location.hash then its waste of using this method
    – Sandeep
    Commented Apr 1, 2013 at 10:09
  • 2
    I didn't down vote, but that's way more complicated than it needs to be. Commented Apr 1, 2013 at 10:10
  • @DavidThomas I agree with you. Updated the answer.
    – Jaya Mayu
    Commented Apr 1, 2013 at 10:11
  • 1
    Weird voting on this question. The correct answer gets two downvotes, while the convoluted answer, later changed to copy the correct answer gets two upvotes.
    – gilly3
    Commented Apr 1, 2013 at 18:25
-1

You could use this

h=new URL(location).hash.split`&`.find(e=>/hash_name/.test(e)).split`=`[1]
1
  • Why is this bad?
    – DerpyCoder
    Commented Jul 28, 2020 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.