Google API Loader setOnLoadCallback() - DOM 准备好了吗?

发布于 2024-09-30 18:21:50 字数 1491 浏览 0 评论 0原文

我正在使用 SWFObject,对于替代内容(无 Flash),我想使用 jQuery 插件。
显然我只想在 Flash 不可用时加载 jQuery 和插件脚本。所以 Google 的 API Loader 看起来很完美。

现在我遇到了 setOnLoadCallback() 事件的问题。它似乎应该被触发,但也许是在 DOM 准备好之前? 我发现另一个问题表明有一个第二个未记录的参数,在 DOM 加载时..
但我仍然无法访问 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>

编辑:似乎像 jQuery 这样的库的 google.load 只能在 window.load 上使用
只有少数 Google 自有的 API 可以通过回调动态加载
请参阅:Google Library API - google.load 不从事件加载?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-10-07 18:21:50

我怀疑当调用 jQueryLoaded 时 DOM 实际上还没有准备好。您应该确保从使用 swfobject.addDomLoadEvent 注册的回调中调用 swfobject.embedSWF

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.

翻身的咸鱼 2024-10-07 18:21:50

@MPD 注意到 DOM 尚未准备好,因此我使用了 swfobject.addDomLoadEvent 和回调。
在这里使用 google.load("jquery", "1.4.3"); 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>

@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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文