Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

jquery - Is it ok to use google.setOnLoadCallback multiple times?

I'm building a site using ASP.NET MVC, and I have partial views that use jQuery to do various things. I was thinking of switching to Google's AJAX API and using their loader to load jQuery. However, I noticed that I would no longer be able to use $(document).ready() anymore because Google's loader specifies a callback google.setOnLoadCallback().

This is a little bit of a problem for me because I have $(document).ready() in various partial views because they do different things specific to themselves that I don't want the parent view to be aware of.

Can I specify multiple callbacks and just swap out the $(document).ready()'s with google.setOnLoadCallback(someUniqueCallbackFunction)?

Would that be the ideal way to handle this situation or is there something else that is preferred?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you may use setOnLoadCallback in lieu of $(document).ready. There is an undocumented SECOND PARAMETER (or at least, I can't find it) that specifies when to call the callback function; possible values are "false" (default) - on the window load, or "true" - on DOM load (DOMContentLoaded). The DOM event fires once all the markup has loaded (much earlier than window.load). The window load event fires after all the images and scripts and such have finished loading.

// Very similar to $(document).ready()
google.setOnLoadCallback( OnLoad, true );

// Very similar to $(window).load()
// Same as google.setOnLoadCallback( OnLoad, false );
google.setOnLoadCallback( OnLoad );

Yes, you may use setOnLoadCallback multiple times on a single page. This is a very important undocumented feature of the AJAX API (undocumented as of this posting). Every time you call setOnLoadCallback, it stacks up all the functions to be called once the DOM or window loads.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...