开发者

Google API Loader setOnLoadCallback() - is DOM ready?

开发者 https://www.devze.com 2023-01-23 15:31 出处:网络
I am using SWFObject and for the alternative content (no Flash) I want to use a jQuery plugin. Obviously I want to load jQuery and the plugin script only when Flash is not available. So Google\'s API

I am using SWFObject and for the alternative content (no Flash) I want to use a jQuery plugin.

Obviously I want to load jQuery and the plugin script only when Flash is not available. So Google's API Loader seems perfect.

Now I am having issues with the setOnLoadCallback() event. It seems to be triggered like it should, but maybe before the DOM is ready? I found another question on SO revealing there is a second undocumented parameter, on DOM load..

but I still can't access jQuery!

<script type="text/javascript" src="https://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
    google.load("swfobject", "2.2");
</script>
<script type="text/javascript">
    swfobject.embedSWF("slideshow.swf", "slideshow", "800", "530", "7","expressInstall.swf", null, null, null, flashNotLoaded);

    function flashNotLoaded(e) {
        if (!e.success) {
            google.load("jquery", "1.4.2");
            google.setOnLoadCallback(jQueryLoaded, true);
        }
    }

    function jQueryLoaded() {
        alert("jquery loaded");
        $("body").css("background-开发者_如何学编程color","ff00ff"); // does not work....
        $(function() {
            $("body").css("background-color","ff0000"); // neither does this...
        });
    }
</script>

EDIT: It seems that google.load for libraries like jQuery is only available on window.load

Only a few of Google own API's can be dynamically loaded with callbacks

See: Google Library API - google.load does not load from event?


I suspect the DOM isn't actually ready when jQueryLoaded is called. You should probably make sure swfobject.embedSWF is called from a callback registered with swfobject.addDomLoadEvent.


@MPD noticed the DOM is not ready, so I used swfobject.addDomLoadEvent with a callback.
In here with google.load("jquery", "1.4.3"); jQuery doesn't seem to load?

<script type="text/javascript" src="https://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
    google.load("swfobject", "2.2");
    google.setOnLoadCallback(swfobjLoaded);

    function swfobjLoaded() {
        swfobject.embedSWF("slideshow.swf", "slideshow", "800", "530", "7","expressInstall.swf", null, null, null, swfobjSuccess);
        swfobject.addDomLoadEvent(swfobjDOMReady);
    }

    var isSWFEmbedded = true;
    function swfobjSuccess(e) {
        if (!e.success) {
            isSWFEmbedded = false;
        }
    }

    function swfobjDOMReady() {
        if (!isSWFEmbedded) {
            alert("dom is ready, Flash is not embedded, now load jquery"); // everything works fine untill here
            google.load("jquery", "1.4.3"); // does not load, page goes blank??
            google.setOnLoadCallback(jqueryLoaded);
        }
    }

    function jqueryLoaded() {
        $("body").css("background-color","ff0000");
    }
</script>
0

精彩评论

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