Handle URL Anchor Change event in JS Monitor the address bar in the back of #

Source: Internet
Author: User

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, scrollbars aren ' t di Splayed.

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 have a plugin that would check for the Hashchange event and provide its own if necessary-http://benalman.com/p rojects/jquery-hashchange-plugin/.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.