最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get string after '#' in URL - Stack Overflow

programmeradmin3浏览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/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
Add a ment  | 

6 Answers 6

Reset to default 5

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

发布评论

评论列表(0)

  1. 暂无评论