Search code examples
javascriptyui3

How to ensure YUI object to be created first


I like YUI sandbox but I have a problem with it. Consider a document with an iframe (for instance ckedior that I included) with an initialization script. It is always executed before the outer document's sections (don't know why).

And I need to call a function, that is initialized in YUI sandbox in the parent document. But it can never be initialized, because the execution starts in the iframe.

var getWordCount;

AUI().ready('aui-node', 'console', function(A) {

    getWordCount = function (htmlData) {
        var target = A.one('div.my-png-image');
        target.one('div:fist-child').set('text', strip(htmlData).trim().split(/\s+/).length);
    };
});

Without yui sandbox I'd just declared a function in the parent document, and even tough the execution starts in the iframe, the global function would exist and could be called from the iframe.


Solution

  • The problem might be AUI.ready, as it executes only when the whole HTML is loaded. This means the iFrame would load before the browser triggers the .ready-event.

    You could try to test this by changing the AUI event you use to trigger your code, and put the code you want to run before the iFrame-load outside the AUI.ready.

    If you need a certain element in dOM to be loaded before running, you could try checking if the element is loaded and then run your script, like in this example.