开发者

jquery document ready with Google

This is how I load jQuery:

<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function OnLoad() {
   insert jQuery goodness here
};
google.load("jquery", "1");
google.setOnLoadCallback(OnLoad);
</script>

But instead of function OnLoad() {, I'd like to use

$(document).ready(fun开发者_StackOverflowction() {}

so that it's like every example in every book and documentation snippet.

How can I define: $ = jQuery?


Why not just abolish the google loady bit and do this, like us eccentric cool kids:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    alert("look ma I'm normal");
});
</script>


You should not have to do anyhting special. This is what we use and it just works:

<script src="http://www.google.com/jsapi?key=snip" type="text/javascript"></script>
<script type="text/javascript">
    google.load("jquery", "1");
    google.load("jqueryui", "1");
</script>
<script type="text/javascript">
    $(document).ready(function() {
      //goodness
    });
</script>

Note that the jQuery script block comes after google.load()


Did you try doing jQuery goodness after you've loaded it?

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1.4.2");</script>
<script type="text/javascript">
    $(function(){ alert("!"); }); // same as $(document).ready(function(){});
</script> 

You seem to be forced to use one of the jQuery versions listed here.


Since all good answers here, but no one checked!? %-), i just post a jquery + google apis exercise!

you may find sometimes usefull! ;-)

Google Map - Current location

UPDATE:

ok, just for make things clear,

nothing special with setOnLoadCallback ; it's just a google way of doing:

inline body + js

<body onload="callback()">

OR clasic js

window.onload = callback;

OR

window.onload = function(){ callback(); };

OR jquery onload

$(window).bind("load", function(){});

OR

$(window).load( function() { /*do something onload */});

OR jquery DOM ready

$(document).ready(function(){ /*do something on dom ready */ });

OR

$(function() { /*do something on dom ready */}); 

All theese work the same of Google Load OR better Google Load load the same fo All theese! ;-)

NOTE: as Matthew Flaschen say, the Load method is not the same of Ready in the sense of, the first can be used for access elements after entire page is loaded (images and other objects that you have attached into the page), the second can be used for access elements after the DOM is ready! hope now is all clear! doh!

so the two method are not the same but in some circumstances you can use it for make similar things!


Adminting setOnLoadCallback will execute the OnLoad function why not doing this :

<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function OnLoad() {
   insert jQuery goodness here
};
google.load("jquery", "1");

var OnLoad = $(document).ready(function() {};

google.setOnLoadCallback(OnLoad);
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜