码迷,mamicode.com
首页 > Web开发 > 详细

Handle URL anchor change event in js 监控地址栏里面的#后面

时间:2014-07-24 12:31:35      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:http   os   strong   io   for   re   c   cti   

Google Custom Search Engines use a timer to check the hash against a previous value, whilst the child iframe on a seperate domain updates the parent‘s location hash to contain the size of the iframe document‘s body.  When the timer catches the change, the parent can resize the iframe to match that of the body so that scrollbars aren‘t displayed.

Something like the following achieves the same:

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, Opera 10.60, Firefox 3.6 and Internet Explorer 8 all support the hashchange event:

if ("onhashchange" in window) // does the browser support the hashchange event?
    window.onhashchange = function () {
        hashChanged(window.location.hash);
    }

and putting it together:

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 also has a plugin that will check for the hashchange event and provide its own if necessary - http://benalman.com/projects/jquery-hashchange-plugin/.


Handle URL anchor change event in js 监控地址栏里面的#后面,布布扣,bubuko.com

Handle URL anchor change event in js 监控地址栏里面的#后面

标签:http   os   strong   io   for   re   c   cti   

原文地址:http://my.oschina.net/122612475/blog/294422

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!