开发者

Jquery calling function on hashchange

开发者 https://www.devze.com 2023-03-16 03:41 出处:网络
I have 2 questions I am using the hashchange plugin .... so I want to know would a function as below, be called everytime a hashchange occurs... because I have something like that in my code and th

I have 2 questions

  1. I am using the hashchange plugin .... so I want to know would a function as below, be called everytime a hashchange occurs... because I have something like that in my code and the code function apparently doesnt seems to be called

    $(document).ready(function()
    {
        // function here
    });
    
  2. On the other have if I remove the hashchange as in If i make http://abc.com/a.htm#http://abc.com/b.htm as http://abc.com/b.htm the code works fine

the problem is the structure of my pages is a bit different .... here is the fiddle with the page structure that explains on a higher level what I am trying to achieve jsfiddle.net/vBKWd/9 ... on hash ch开发者_Python百科ange jus the div c on my page 1 gets replaced by page 2 and vice versa .... and the js function that I have shown below is getting called only once and not after hashchange

Or is therre any way I can bind the function with the div so that whenever the div is replace the function get called?


No, a ready handler is only called on document ready, not on hash change. You should use the hashchange event for that, instead:

$(window).hashchange(function () {
    // function here
});

Sample: http://jsfiddle.net/vBKWd/2/


In document ready wirte code below

$(window).bind('hashchange', function () {
                 //code here
});


use live in this case

$(document).ready(function()
{   
   $(selector).live(hashchange, function(){
    // your code goes here

    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号