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 youwindow.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
2 Answers
Reset to default 6It'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
本文标签:
javascriptGet the text fragment part of current URL from windowlocationStack Overflow
版权声明:本文标题:javascript - Get the text fragment part of current URL from window.location - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1742016502a2413872.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
:~:
appears in the URL.
发表评论