I am trying to get the string available after #
in the URL.
basically its an ID of the element that is passed from other page.
for example, the below url has investors_brand after #
character, i need to get that string with jquery
www.example/about_us#pany_branding
here is the code.
var a = window.location.href;
console.log(a.slice('#'));
But could not get it right.
I am trying to get the string available after #
in the URL.
basically its an ID of the element that is passed from other page.
for example, the below url has investors_brand after #
character, i need to get that string with jquery
www.example./about_us#pany_branding
here is the code.
var a = window.location.href;
console.log(a.slice('#'));
But could not get it right.
Share Improve this question asked May 7, 2019 at 9:11 CeejayCeejay 7,26719 gold badges65 silver badges109 bronze badges 2- Possible duplicate of How to split a string after a particular character in jquery – adiga Commented May 7, 2019 at 9:14
- Possible duplicate of How do I get the fragment identifier (value after hash #) from a URL? – Mohammad Commented May 7, 2019 at 9:47
6 Answers
Reset to default 5You can get the hash
value:
window.location.hash
If you want it without the #
use:
window.location.hash.substring(1)
Use split
console.log('www.example./about_us#pany_branding'.split('#')[1])
var a = window.location.href;
console.log(a.split('#')[1]);
use window.location.hash
console.log(window.location.hash)
You could use substring
.
const hash = window.location.hash;
console.log(hash.substring(1));
This returns the part of the string after the index 1
Try
var a = 'abc./blog/seo-google-123';
console.log(a.split('-').pop());
Result: 123
You can use the hash value:
https://www.w3schools./jsref/prop_loc_hash.asp
var x = location.hash;