简体   繁体   English

Google API加载程序setOnLoadCallback()-DOM准备好了吗?

[英]Google API Loader setOnLoadCallback() - is DOM ready?

I am using SWFObject and for the alternative content (no Flash) I want to use a jQuery plugin. 我正在使用SWFObject,对于替代内容(没有Flash),我想使用jQuery插件。
Obviously I want to load jQuery and the plugin script only when Flash is not available. 显然,我只想在Flash不可用时才加载jQuery和插件脚本。 So Google's API Loader seems perfect. 因此,Google的API加载程序似乎很完美。

Now I am having issues with the setOnLoadCallback() event. 现在,我遇到了setOnLoadCallback()事件的问题。 It seems to be triggered like it should, but maybe before the DOM is ready? 它似乎应该触发,但是可能在DOM准备就绪之前? I found another question on SO revealing there is a second undocumented parameter, on DOM load.. 我在SO上发现了另一个问题该问题表明在DOM负载上还有第二个未记录的参数。
but I still can't access jQuery! 但我仍然无法访问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 编辑:似乎像jQuery这样的库的google.load仅在window.load上可用
Only a few of Google own API's can be dynamically loaded with callbacks Google自己的API中只有少数可以通过回调动态加载
See: Google Library API - google.load does not load from event? 请参阅: Google Library API-google.load不会从事件加载吗?

I suspect the DOM isn't actually ready when jQueryLoaded is called. 我怀疑调用jQueryLoaded时DOM尚未真正准备好。 You should probably make sure swfobject.embedSWF is called from a callback registered with swfobject.addDomLoadEvent . 你或许应该确保swfobject.embedSWF是从注册一个回调称为swfobject.addDomLoadEvent

@MPD noticed the DOM is not ready, so I used swfobject.addDomLoadEvent with a callback. @MPD注意到DOM尚未准备就绪,因此我将swfobject.addDomLoadEvent与回调一起使用。
In here with google.load("jquery", "1.4.3"); 在这里使用google.load("jquery", "1.4.3"); jQuery doesn't seem to load? 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");
    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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM