admin管理员组

文章数量:1317123

I wanted to have a custom layout for anyone visitsing my website with text fragment link.

eg. .php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats

Here the part after :~: will be the text fragment. I wanted to get that part.

I have tried window.location.href and window.location.hash. But window.location.href returns just the url without text fragment while window.location.hash returns an empty string.

To replicate this visit this .php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats by copying and pasting it to new tab, and try getting the text fragment from window.location (markdown link seems to have problem with text fragment).

So is there any other way to get the text fragment part in address bar ?

I wanted to have a custom layout for anyone visitsing my website with text fragment link.

eg. https://en.wikipedia/w/index.php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats

Here the part after :~: will be the text fragment. I wanted to get that part.

I have tried window.location.href and window.location.hash. But window.location.href returns just the url without text fragment while window.location.hash returns an empty string.

To replicate this visit this https://en.wikipedia/w/index.php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats by copying and pasting it to new tab, and try getting the text fragment from window.location (markdown link seems to have problem with text fragment).

So is there any other way to get the text fragment part in address bar ?

Share Improve this question edited Apr 10, 2021 at 22:38 GAGANDEEP SINGH asked Apr 10, 2021 at 21:58 GAGANDEEP SINGHGAGANDEEP SINGH 1,0362 gold badges12 silver badges15 bronze badges 6
  • 1 .hash will contain it – Lawrence Cherone Commented Apr 10, 2021 at 22:03
  • 2 I have tried in both firefox and chrome. It doesn't seems to contain. window.location.hash just returns an empty string. Does it work for you window.location.hash on that wikipedia page ? – GAGANDEEP SINGH Commented Apr 10, 2021 at 22:10
  • yes, how are you executing it? – Lawrence Cherone Commented Apr 10, 2021 at 22:12
  • It has the hash but I don't know why the text fragments are not working when clicking from the markdown link directly. When clicking on the markdown link it open wikipedia but also doesn't select the text which text fragments are for. So please open the link by copy pasting https://en.wikipedia/w/index.php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats from here and then try getting hash – GAGANDEEP SINGH Commented Apr 10, 2021 at 22:30
  • empty hash string – GAGANDEEP SINGH Commented Apr 10, 2021 at 22:37
 |  Show 1 more ment

2 Answers 2

Reset to default 6

It's currently different based on browser.

Firefox

window.location.href and window.location.hash work as they have worked since the dawn of Javascript and behave exactly as you would expect.

Chrome

Recent changes have modified fragment behavior. Chrome will now remove parts of the URL if :~: appears in the URL.

本文标签: javascriptGet the text fragment part of current URL from windowlocationStack Overflow