Load external script dynamic, and access it, so I can trigger its domready function

Posted by Didier on Stack Overflow See other posts from Stack Overflow or by Didier
Published on 2010-04-12T15:57:45Z Indexed on 2010/04/13 8:23 UTC
Read the original article Hit count: 474

Filed under:
|

I am trying to load the Zopim chat client. Normally it is included with a document write action. This causes it to be loaded before the domReady function is triggered, as it needs this to start itself.

I want to load it later, and this works by using prototype (framework determined by Magento) to create a new script element and attaching it to the head. The script is loaded perfectly, but the domReady doesn't fire, so the script is never started.

The script is a nameless class, by this I mean that all its functions are encapsulated in {} UPDATE: Sorry, I got it wrong, it is as self invoking function, the same syntax as the first answer suggest. (function C(){ })();

This function call when run sets up listening events for the domReady event under various browsers and then waits. When the domready event fires, it calls a function (that is within the self-invoked function) that starts everything. What I need, is a way to access this function somehow. END UPDATE

Within that is a function named C.

How can I call this function directly?

Or put another way, how can I start an external javascript file that depends on domready going off, when that event doesn't happen?

Can I load an external javascript file into a variable, so I can name the class? Can I access the nameless class {} somehow maybe via the script tag? Is there a way to alter the external file/javascript so I can have it look for another event, one that I can trigger?

About the only solution I can think of at the moment is to create a iframe and load the script in that.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototype