最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reload $(document).ready(); functions when AJAX has loaded - Stack Overflow

programmeradmin0浏览0评论

I am currently skinning an application where I have no access to the markup or scripts apart from one CSS file and on JS file so I can add my own styles and functions. Not an ideal set-up I know but believe me it's not my choice. Oh and all the content on this site is generated via AJAX requests, nice.

Anyway, the problem is that I have a lot of Javascript that needs to run on $(document).ready(); but as the page never really reloads (due to the AJAX), none of the code gets executed.

Is there a way I can set all the functions in $(document).ready(); to run everytime the AJAX call has been pleted?

I've included the built in AJAX call (that I cannot edit) below.

$.ajax({
  type: "POST",
  dataType: "html",
  url: url1,
  data: pars,
  success: function(data)
  {
    $('#thisContent').html(data);
    loadMenu(urlToLoad);
  } 
});

I am currently skinning an application where I have no access to the markup or scripts apart from one CSS file and on JS file so I can add my own styles and functions. Not an ideal set-up I know but believe me it's not my choice. Oh and all the content on this site is generated via AJAX requests, nice.

Anyway, the problem is that I have a lot of Javascript that needs to run on $(document).ready(); but as the page never really reloads (due to the AJAX), none of the code gets executed.

Is there a way I can set all the functions in $(document).ready(); to run everytime the AJAX call has been pleted?

I've included the built in AJAX call (that I cannot edit) below.

$.ajax({
  type: "POST",
  dataType: "html",
  url: url1,
  data: pars,
  success: function(data)
  {
    $('#thisContent').html(data);
    loadMenu(urlToLoad);
  } 
});
Share Improve this question asked Oct 11, 2012 at 11:02 Sam BeckhamSam Beckham 1,2182 gold badges12 silver badges23 bronze badges 1
  • 1 You could call the $(document).ready() handler at the end of the success handler. – user1726343 Commented Oct 11, 2012 at 11:05
Add a ment  | 

3 Answers 3

Reset to default 4

Yes, you can add a handler for .ajaxComplete(). It is called after every ajax call is finished (either successfully or with error).

Further reading: Ajax Events

If what you want is for the handler of the $(document).ready([handler]) event to be called again you could just use a named function as handler, and reference that:

$(document).ready(function readyHandler()
{
});
//and:
$.ajax({....,success:readyHandler});

But, personally, I'd remend you to think about this. If, for example, the $.ajax call is made in the readyHandler function, you're going to create a deadlock. You're also quite likely to make the same binds over and over again. I don't know what you're trying to achieve but I'm pretty sure that there's a better way to do it. (If indeed I understood your question correctly)

I think this can be useful for you.

发布评论

评论列表(0)

  1. 暂无评论