开发者

Modify url for bookmarking using JavaScript

开发者 https://www.devze.com 2022-12-31 03:55 出处:网络
I\'m exploring my options for modifying urls in the browser bar for bookmarking purposes. Ideally, I\'d like to add querystring parameters and cannot determine if this is even possible.I don\'t want

I'm exploring my options for modifying urls in the browser bar for bookmarking purposes.

Ideally, I'd like to add querystring parameters and cannot determine if this is even possible. I don't want the page to refresh and want to add querystring values on link clicks, ajax calls, etc.

If I can't add querystring parameters, then I'd like to add hash values (http:://someurl.com#hash-value). How should I go about doing this? Should I use plain JavaScript or a framework (jquery开发者_Go百科, prototype, etc.) and/or framework plugin.


To modify the hash, you can simply do the following in plain JavaScript:

window.location.hash = 'hash-value';

It will add #hash-value to your URL, or will replace it if it already exists, without refreshing the page.

Then to check if a hash value is present, simply do the following:

if (window.location.hash) {
    // Hash is present
    // Use window.location.hash as required
}
else {
    // No hash was set
}


If you modify the query string, it will refresh. So you should modify window.location.hash.

0

精彩评论

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