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.

2

6 Answers 6

5

You can get the hash value:

window.location.hash

If you want it without the # use:

window.location.hash.substring(1)
Sign up to request clarification or add additional context in comments.

1 Comment

Basically what @apple said. I was composing this answer at the same time.
4

Use split

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

var a = window.location.href;
console.log(a.split('#')[1]);

1 Comment

hard index, not should.
4

use window.location.hash

console.log(window.location.hash)

Comments

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

Comments

1

Try

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

Result: 123

1 Comment

although you can directly get hash value of URL by window.location.hash
0

You can use the hash value:

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

var x = location.hash;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.