GhostWriter

Summary
GhostWriter
ghostwriterRuns a script within an simulated pre-load environment, handling onload event notification and document.write() output.
config attributes
script{Function|Object} Defines the script which will be attached or executed under the pre-load emulation context.
insertType{Function|String} Defines how output created by script should be added to <element>.
done{Function} Callback function to run after the script and all children have been loaded.
Examples
SimpleRun a JS script that appends to the #writeto element.
Remote scriptLoads a script and insert any content before the first script tag on the page.
Utility Functions
Functions
flogLogs a message to the YUI console or browser console if available

ghostwriter

Runs a script within an simulated pre-load environment, handling onload event notification and document.write() output.

function ghostwriter(
     HTMLELement  element,
     Object config
);

Note: See ghostwriter.loadscripts for information on how to use HTML tags to defer all scripts.

Returns

null

Parameters

element{HTMLElement} The Element around which to insert the DOM elements.  Can also be the id of an element.
config{Object} Specifies the config options for execution, including the script itself .

config attributes

script

{Function|Object} Defines the script which will be attached or executed under the pre-load emulation context.

FunctionWill execute
ObjectA HTMLScriptElement will be created and attached to the DOM.  Must contain either
url - The url of a remote script resource
text - The text of a script to execute

insertType

{Function|String} Defines how output created by script should be added to <element>.

FunctionCalled every time a new HTML tag is encountered.
StringMust be one of the following (Default is “after”).
"append" - Appends to element
"before" Inserts before element
"after" Inserts immediately following element

done

{Function} Callback function to run after the script and all children have been loaded.

Examples

Simple

Run a JS script that appends to the #writeto element.

ghostwriter(
    "writeto",
    {
         insertType: "append",
         script: { text: "document.write('<h4>Hello, world</h4>');" },
    }
);

The #writeto element will now have this HTML at the end

<h4>Hello, world</h4>

Remote script

Loads a script and insert any content before the first script tag on the page.  When complete, execute any onload observers registered during execution

var scriptMap= document.getElementsByTagName("SCRIPT")[0],
ghostwriter(
    scriptMap[scriptMap.length],
    {
        insertType: "before",
        script: { url: "/js/mylib.js" },
        done: ghostwriter.flushloadhandlers
    }
);

/js/mylib.js

+------------------------------------------------------------------
| document.write('<link href="...." rel="stylesheet" type="text/css"/>');
| document.write('<script src="http://ajax.googleapis..."><\/script>');
| document.addEventListener("load", function(){ ... } );
------------------------------------------------------------------

Only after the ajax.googleapis script has finished loading will the ghostwriter.flushloadhandlers() method be run.

Utility Functions

Summary
Functions
flogLogs a message to the YUI console or browser console if available

Functions

flog

function flog(msg)

Logs a message to the YUI console or browser console if available

Parameters

msgThe message to log

Returns

null

function flog(msg)
Logs a message to the YUI console or browser console if available
Iterates over all script tag searching for those with a type of “text/deferscript” and queues them one-at-a-time to be executed.
Close