[英]Urls for results of Ajax requests
嗨,大家好,我开发了一个具有整洁的搜索功能的google maps应用程序-但是,我注意到我想要做一个像Facebook的家伙那样做的事情,就像硬链接到ajax查询生成的页面一样。 例如,您单击facebook中的链接,并将其附加到当前URL,然后可以复制粘贴新的URL以获取所请求的页面...我该如何实现类似的功能...
听起来好像您想使用链接来更新浏览器的url,该链接可用于以当前状态重新访问该页面。 可以通过操纵浏览器历史记录来完成。
一个好的解决方案是:
真正简单的历史
http://code.google.com/p/reallysimplehistory/
Really Simple History是一个轻量级的JavaScript库,用于管理Ajax / DHTML应用程序中的书签和浏览器历史记录。 RSH在内部JavaScript缓存中序列化应用程序数据,以便可以使用书签和后退按钮使您的应用程序返回到较早的状态。
看看这个出色的例子:
http://www.justise.com/2009/01/26/enabling-the-back-button-in-ajax-applications/
您可以使用location.hash
(#后面的URL部分)来设置JavaScript的“硬链接”。 这不会像更改location.href
那样导致重新加载页面,但是您可以获取值并在JavaScript中使用它。
考虑以下示例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Hash test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onload = function() {
// The substring(1) removes the # from the hash.
var hash = window.location.hash.substring(1);
// Use a default value if no has was passed.
if(hash == '') {
hash = 'Nothing';
}
// Use the value.
document.getElementById('selection').innerHTML = hash;
}
/** Sets the hash and updates the page value */
function setHash(newHash) {
window.location.hash = newHash;
document.getElementById('selection').innerHTML = newHash;
}
</script>
</head>
<body>
<div>
<div>
<a href="javascript: void();" onclick="setHash('page1');">Page 1</a> |
<a href="javascript: void();" onclick="setHash('page2');">Page 2</a> |
<a href="javascript: void();" onclick="setHash('page3');">Page 3</a>
</div>
<div>
You have selected: <span id="selection">...</span>
</div>
</div>
</body>
</html>
当您单击页面链接之一时, location.hash
会更改,浏览器的URL将更新,并且页面中使用的值也会更改。 如果用户然后直接从地址栏中复制URL或将其添加为书签,则再次请求URL时将重新加载所选页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.