0

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.com/about_us#company_branding

here is the code.

var a = window.location.href;
console.log(a.slice('#'));

But could not get it right.

CJAY
  • 6,435
  • 17
  • 58
  • 100
  • Possible duplicate of [How to split a string after a particular character in jquery](https://stackoverflow.com/questions/24156535) – adiga May 07 '19 at 09:14
  • Possible duplicate of [How do I get the fragment identifier (value after hash #) from a URL?](https://stackoverflow.com/questions/11662693/how-do-i-get-the-fragment-identifier-value-after-hash-from-a-url) – Mohammad May 07 '19 at 09:47

6 Answers6

3

Use split

console.log('www.example.com/about_us#company_branding'.split('#')[1])

var a = window.location.href;
console.log(a.split('#')[1]);
ellipsis
  • 11,688
  • 2
  • 14
  • 33
2

use window.location.hash

console.log(window.location.hash)
apple apple
  • 7,296
  • 1
  • 14
  • 35
1

Try

var a = 'abc.com/blog/seo-google-123';
console.log(a.split('-').pop());

Result: 123

Tran Anh Hien
  • 587
  • 7
  • 11
0

You can use the hash value:

https://www.w3schools.com/jsref/prop_loc_hash.asp

var x = location.hash;

Fribu - Smart Solutions
  • 2,734
  • 3
  • 27
  • 60
0

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

0

You can get the hash value:

window.location.hash

If you want it without the # use:

window.location.hash.substring(1)
Alex
  • 8,707
  • 2
  • 26
  • 44