Hello

Please suggest: how, say, when navigating to site.ru/dir/#func , one function is performed, and for site.ru/dir/#func2 , another jquery function.

I tried click() , but there is no effect.

Thank you in advance.

  • one
    google hashchange event or use specialized routing library - Specter

3 answers 3

If you want the function to work when the page loads depending on the hash:

 function func1 { ... } function func2 { ... } if(window.location.hash == "#func1") { func1(); } else if(window.location.hash == "#func2") { func2(); } 

Handler for hash changes on the page:

 $(window).bind('hashchange', function() { if(window.location.hash == "#func1") { func1(); } else if(window.location.hash == "#func2") { func2(); } }); 

    On the site.ru/dir/ page site.ru/dir/ put the script:

     $(function(){ var h = location.hash; if(h == "func") { ... } if(h == "func2") { ... } }) 

      Code scrolling on the link if url with an anchor. It may be clumsy and not universal, but it works.

       function hashcontrol () { var otstup=100 //если есть верхний колонтитул с position:fixed var speed=1000 //скорость анимации var scrollof = window.location.hash.split("#")[1]; if (scrollof) { scrollof=$("a[name^="+scrollof+"]") ; $("html:not(:animated)" +( !$.browser.opera ? ",body:not(:animated)" : "")).animate( { scrollTop: scrollof.offset().top - otstup}, speed ); } } $(document).ready(function(){ hashcontrol (); $(window).bind('hashchange', function() { hashcontrol ();}); });