239

Example:

www.site.com/index.php#hello

Using jQuery, I want to put the value hello in a variable:

var type = …
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
cppit
  • 4,478
  • 11
  • 43
  • 68
  • I’m uncertain, whether this question should remain being tagged as jQuery and asking about it. Most of the answers apply to JS without jQuery and this seems like a good dupe target. – Sebastian Simon Mar 02 '18 at 02:41

8 Answers8

637

No need for jQuery

var type = window.location.hash.substr(1);

Since String.prototype.substr is deprecated use substring instead.

var type = window.location.hash.substring(1);
Musa
  • 96,336
  • 17
  • 118
  • 137
37

You may do it by using following code:

var url = "www.site.com/index.php#hello";
var hash = url.substring(url.indexOf('#')+1);
alert(hash);

SEE DEMO

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51
  • 4
    Note: indexOf is not supported by IE8 – Sebastiaan Ordelman Jun 10 '15 at 14:36
  • 4
    @SchalkKeun luckily, now in '18 it's almost gone. But for those who still have to deal with that legend should be aware of it. – Sebastiaan Ordelman Jun 22 '18 at 09:45
  • 7
    To be honest, IE8 is so insecure, all the payment gateways basically is going to drop any browser not supporting TLS 1.2 next week (30 June 2018), you will not be able to do payments from them at all. So that makes all these old crappy browsers useless for any ecommerce client. – Schalk Keun Jun 22 '18 at 12:05
13
var url ='www.site.com/index.php#hello';
var type = url.split('#');
var hash = '';
if(type.length > 1)
  hash = type[1];
alert(hash);

Working demo on jsfiddle

Talha
  • 18,898
  • 8
  • 49
  • 66
8

It's very easy. Try the below code

$(document).ready(function(){
  var hashValue = location.hash.replace(/^#/, '');  
  //do something with the value here  
});
ByteFlowr
  • 511
  • 7
  • 16
kmario23
  • 57,311
  • 13
  • 161
  • 150
8

Use the following JavaScript to get the value after hash (#) from a URL. You don't need to use jQuery for that.

var hash = location.hash.substr(1);

I have got this code and tutorial from here - How to get hash value from URL using JavaScript

JoyGuru
  • 1,803
  • 20
  • 11
5

I had the URL from run time, below gave the correct answer:

let url = "www.site.com/index.php#hello";
alert(url.split('#')[1]);

hope this helps

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
3

Get fragment of current document location

var hash = window.location.hash;

Get fragment from string

// absolute
var url = new URL('https://example.com/path/index.html#hash');

console.log(url.hash);

// relative (second param is required, use any valid URL base)
var url2 = new URL('/path/index.html#hash2', 'http://example');

console.log(url2.hash);
Arthur Shlain
  • 961
  • 10
  • 23
2

Based on A.K's code, here is a Helper Function. JS Fiddle Here (http://jsfiddle.net/M5vsL/1/) ...

// Helper Method Defined Here.
(function (helper, $) {
    // This is now a utility function to "Get the Document Hash"
    helper.getDocumentHash = function (urlString) {
        var hashValue = "";

        if (urlString.indexOf('#') != -1) {
            hashValue = urlString.substring(parseInt(urlString.indexOf('#')) + 1);
        }
        return hashValue;
    };
})(this.helper = this.helper || {}, jQuery);
Rohit L
  • 1,441
  • 13
  • 12