Is it ok to use google.setOnLoadCallback multiple times?
Asked Answered
L

4

11

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?

Lashkar answered 4/9, 2009 at 15:45 Comment(2)
For these partial views, are you rendering them with the page or dynamicaly (e.g., through an ajax tab)?Vyner
In a regular page. I'm not using AJAX at this point.Lashkar
V
0

I may be missing the point here but there's no clash between your usercontrols $(document).ready and google.setOnLoadCallback that I know of.

Assuming you're using google to load jquery, your code in the $(document).ready won't run until google has loaded jquery anyway.

So long as jQuery is being loaded in your masterpage, not sure what the issue is.

Vaas answered 5/9, 2009 at 16:13 Comment(2)
I'll try that out and see if it works. I tried using the setOnLoadCallback and $(document).ready in the same html file on the Google API Playground and it didn't like that. Maybe I'm ok with user controls, though!Lashkar
That seems to work fine. I have a Master file that uses google.load(jquery) and then later in a view I'm using $(document).ready and it's working fine!Lashkar
R
32

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.

Rasorial answered 27/10, 2009 at 7:35 Comment(3)
This is what helped me. I wish this were better documented.Potvaliant
You just made my day.Hohenstaufen
It is now documented that setOnLoadCallback can be called multiple times.Reimpression
O
2

I ran into the same problem. I googled have 2 google search running on one page, etc...

In the end i found this and because i wanted to have 2 panels essentially showing google search results on one page.

google.setOnLoadCallback(LoadGoogleNewsResults);
google.setOnLoadCallback(LoadGoogleNewsResultsForum);

This worked for me :)

The code is on http://login.debt-line.org.uk, just click view source.

Oneself answered 20/7, 2010 at 12:53 Comment(0)
V
0

I may be missing the point here but there's no clash between your usercontrols $(document).ready and google.setOnLoadCallback that I know of.

Assuming you're using google to load jquery, your code in the $(document).ready won't run until google has loaded jquery anyway.

So long as jQuery is being loaded in your masterpage, not sure what the issue is.

Vaas answered 5/9, 2009 at 16:13 Comment(2)
I'll try that out and see if it works. I tried using the setOnLoadCallback and $(document).ready in the same html file on the Google API Playground and it didn't like that. Maybe I'm ok with user controls, though!Lashkar
That seems to work fine. I have a Master file that uses google.load(jquery) and then later in a view I'm using $(document).ready and it's working fine!Lashkar
W
0

You can do something like below:

google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawAllCharts);


function drawAllCharts()
{
   chart1();
   chart2();
   ...
}
Wyn answered 5/8, 2022 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.