开发者

Read window.location.hash servlet-side not possible?

开发者 https://www.devze.com 2023-01-06 22:44 出处:网络
In my web app, a user can click an item in a list, and I modify the url in their browser: <li>Horse</li>

In my web app, a user can click an item in a list, and I modify the url in their browser:

<li>Horse</li>
<li>Cow</li>
<li>Goat</li>

function onListItemClicked() {
    window.location.hash = item.name;
}

this will change the url in the user's browser to:

www.example.com#Horse

www.example.com#Cow

www.example.com#Goat

if I'm reading correctly, we can't get the # part of the url servlet-side, right? If the user copies and pastes the url from their browser to friend, it would b开发者_JAVA技巧e cool if I could generate the page already initialized with the item they clicked.

It looks like this is not possible, I'll have to load the appropriate page via javascript after the document finishes loading,

Thanks


No, you can't do this from the server side on. URL fragments are purely client side. You need to do this in the client side during page load.

window.onload = function() {
    var hash = window.location.hash;
    // Do your business thing here based on the hash.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消