views:

86

answers:

1

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.

A: 

If you want to fire it after loading it asynchronously after the domready is already fired, you can do this:

Make your function C a self invoking function by:

(function C(){
})();

With this approach you would be changing the external script though.

Rajat
This wouldn't be ideal, the external script does not belong to me, and the entire script is it self a self invoking function as you described, and I need to call a function within that, or trigger domready somehow.
Didier
Not sure what you mean by "nameless class", can you provide a simple example of what the javascript looks like?Also, RE the previous comment, making a self-invoking function does NOT mean it will wait for anything, it has nothing to do with DOM ready.
InfinitiesLoop
Unfortunately, I don't think you can load it later and get the function to execute without modifying the script.The script is designed in a way to trigger on domready. When you include it later by injecting via the <script> tag, domready has already triggered. Triggering domready again is equivalent to injecting the script beforehand using document.write.The other option is to load the external JS into a variable somehow. I am not aware of such techniques. Since script has an anonymous object, you also don't get a handle to invoke a function on the object.
Rajat
Thanks, I guess I am going to try loading it in an iframe instead. That should give me both the choice when to load it and still get a domready event on it.Thanks for your advice.
Didier