How do I get the fragment identifier (value after hash #) from a URL?

JavascriptJqueryUrlUrl Fragment

Javascript Problem Overview


Example:

www.site.com/index.php#hello

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

var type = …

Javascript Solutions


Solution 1 - Javascript

No need for jQuery

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

It's very easy. Try the below code

$(document).ready(function(){
  var hashValue = location.hash.replace(/^#/, '');  
  //do something with the value here  
});

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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);

Solution 8 - Javascript

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncppitView Question on Stackoverflow
Solution 1 - JavascriptMusaView Answer on Stackoverflow
Solution 2 - JavascriptAhsan KhurshidView Answer on Stackoverflow
Solution 3 - JavascriptTalhaView Answer on Stackoverflow
Solution 4 - Javascriptkmario23View Answer on Stackoverflow
Solution 5 - JavascriptJoyGuruView Answer on Stackoverflow
Solution 6 - JavascriptManohar Reddy PoreddyView Answer on Stackoverflow
Solution 7 - JavascriptRohit LView Answer on Stackoverflow
Solution 8 - JavascriptArthur ShlainView Answer on Stackoverflow