Google自定义搜索引擎使用计时器来检查哈希值是否与之前的值相对应,而独立域中的子iframe会更新父级的位置哈希值,以包含iframe文档正文的大小。当计时器捕获到更改时,父级可以调整iframe的大小以匹配主体的iframe,以便不显示滚动条。
类似于以下内容可以达到相同目的:
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
storedHash = window.location.hash;
hashChanged(storedHash);
}
}, 100); // Google uses 100ms intervals I think, might be lower
Google Chrome 5,Safari 5,Opera10.60 支持以下hashchange
事件:
if ("onhashchange" in window) // does the browser support the hashchange event?
window.onhashchange = function () {
hashChanged(window.location.hash);
}
并将其放在一起:
if ("onhashchange" in window) { // event supported?
window.onhashchange = function () {
hashChanged(window.location.hash);
}
}
else { // event not supported:
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
storedHash = window.location.hash;
hashChanged(storedHash);
}
}, 100);
}