网址更改时加载ajax

I'm just messing around with ajax for the first time.

At the moment I'm loading text from text files according to the url.

Here's how I'm doing it:

var home_url = "blahblah/index.html#home";
var test_url = "blahblah/index.html#test";

$(document).on("click",".ajax_load",function(){
    if (location.href === home_url) {
        $("#main").load("src/home.txt");
    };
    if (location.href === test_url) {
        $("#main").load("src/test.txt");
    };  
});

This works, however the content isn't loading until after I have pressed the button for the second time. Easier to understand:

button press -> 2nd button press -> content loaded

I think this happens because my code doesn't register the url until after the first press, and then the if statements check whether content needs to load, but this is only triggered until the button is clicked again.

Can anyone help me make the content load with only one button press?

Thanks.

查看全部
weixin_33698823
weixin_33698823
2012/09/19 20:10
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复