1

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,989
  • 18
  • 64
  • 106
  • 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

4

Use split

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

var a = window.location.href;
console.log(a.split('#')[1]);
ellipsis
  • 12,049
  • 2
  • 17
  • 33
4

use window.location.hash

console.log(window.location.hash)
apple apple
  • 10,292
  • 2
  • 16
  • 36
3

You can get the hash value:

window.location.hash

If you want it without the # use:

window.location.hash.substring(1)
Alex
  • 8,875
  • 2
  • 27
  • 44
1

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

1

Try

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

Result: 123

Tran Anh Hien
  • 687
  • 8
  • 11
0

You can use the hash value:

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

var x = location.hash;

Alexander_F
  • 2,831
  • 3
  • 28
  • 61