If I am on https://www.website.com/#something
, how can I return the hash value "something"
from the URL?
Asked
Active
Viewed 2.3k times
10

Sebastian Simon
- 18,263
- 7
- 55
- 75
-
4`window.location.hash` its gives u the hash – Sandeep Apr 01 '13 at 10:07
-
1@Sandeep: that's clearly the answer, why not simply post it *as* an answer? – David Thomas Apr 01 '13 at 10:11
-
@DavidThomas, agreed! – Apr 01 '13 at 10:12
-
posted jus now :) @Christian u can accept it as answer – Sandeep Apr 01 '13 at 10:12
-
PASS VALUE TO PHP??? make an ajax call and send the hash to PHP. – Sandeep Apr 01 '13 at 10:13
-
@Sandeep Why so many question marks? I'm pretty sure my question came across understandable. – Apr 01 '13 at 10:14
-
Nothing simple like `$PHP_Varaibel = document.write(location.hash);` – Apr 01 '13 at 10:16
-
1Looking 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? – David Thomas Apr 01 '13 at 10:24
4 Answers
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(){}
})

Sandeep
- 2,041
- 21
- 34
-
-
ya thats the only way to access a server-side language from browser AFAIK. – Sandeep Apr 01 '13 at 11:26
4
You can use the location.hash property to grab the hash of the current page:
var hash = window.location.hash;

Eli
- 14,779
- 5
- 59
- 77
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

Jay Mayu
- 17,023
- 32
- 114
- 148
-
DOM already provides u a predefined variable location.hash then its waste of using this method – Sandeep Apr 01 '13 at 10:09
-
2I didn't down vote, but that's way more complicated than it needs to be. – David Thomas Apr 01 '13 at 10:10
-
-
1Weird 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 Apr 01 '13 at 18:25
-1
You could use this
h=new URL(location).hash.split`&`.find(e=>/hash_name/.test(e)).split`=`[1]

DerpyCoder
- 127
- 1
- 6