开发者

AJAX and urls with hash tags

开发者 https://www.devze.com 2023-03-31 03:29 出处:网络
I am developing AJAX application and want to make urls with hash tags. When users open such page (someting like #date=27.02.1990&Name=Alex) appl开发者_开发百科ication recovery their state. Is ther

I am developing AJAX application and want to make urls with hash tags. When users open such page (someting like #date=27.02.1990&Name=Alex) appl开发者_开发百科ication recovery their state. Is there any libs which can help me?


History.js will help you achieve this, plus it will use pushState in newer browsers, so it changes the actual URL (without page reload) instead of just changing the fragment identifier.


Something like the Asual jQuery Address plugin should be able to help. It allows you to perform an action when the hash in the url changes. You can use this to load the appropriate content for the data in the hash via ajax.


Something like this could work for you:

// Declare hashobj in global context
// So it can be used anywhere
window.hashobj = {};
var parts = location.hash.substring(1).split('&');

// Strip leading `?`
if(parts[0].lastIndexOf('?', 0) === 0)
    parts[0] = parts[0].substring(1);

// Create global variables
for(var i = 0; i < parts.length; i++){
    parts[i] = parts[i].split('=');
    hashobj[parts[i][0]] = parts[i][1];
}
alert(hashobj.date);

Example

Using that you should be able to read the hash easily enough and make the necessary Ajax requests to load the content.

0

精彩评论

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