如何使用哈希标签创建网址?

我正在开发AJAX应用程序,并希望使用哈希标签创建网址。当用户打开此类页面(诸如#date=27.02.1990&Name=Alex之类的东西)时,应用程序将恢复其状态。有没有可用的库?

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.

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 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.