您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

处理js中的URL锚更改事件

处理js中的URL锚更改事件

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);
}

jQuery的也有一个插件,将检查hashchange事件,并提供了自己的如果需要的话。

其他 2022/1/1 18:15:30 有370人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶