Inserting a script element and executing - Mozilla

This is a discussion on Inserting a script element and executing - Mozilla ; Hello, I am trying to insert a java script element into the body of a document from my toolbar extension. I can insert the script element successfully; but my question is what it the proper way to execute a function ...

+ Reply to Thread
Results 1 to 15 of 15

Thread: Inserting a script element and executing

  1. Inserting a script element and executing

    Hello,

    I am trying to insert a java script element into the body of a document from
    my toolbar extension. I can insert the script element successfully; but my
    question is what it the proper way to execute a function within the element?
    Below is an example of javascript.

    var s = 'function DoIt(){alert("hello");}'
    var contentDocument = window._content.document;
    newScriptElement = contentDocument.createElement('script');
    newScriptElement.setAttribute('id', 'MY_ID');
    newScriptElement.setAttribute('type', 'text/javascript');
    newScriptElement.innerHtml = s;
    contentDocument.body.appendChild(newScriptElement) ;

    DoIt();

    Any suggestions would be greatly appreciated.

    Thanks,
    Tom



  2. Re: Inserting a script element and executing

    Tom wrote:
    > Hello,
    >
    > I am trying to insert a java script element into the body of a document from
    > my toolbar extension. I can insert the script element successfully; but my
    > question is what it the proper way to execute a function within the element?
    > Below is an example of javascript.
    >
    > var s = 'function DoIt(){alert("hello");}'
    > var contentDocument = window._content.document;
    > newScriptElement = contentDocument.createElement('script');
    > newScriptElement.setAttribute('id', 'MY_ID');
    > newScriptElement.setAttribute('type', 'text/javascript');
    > newScriptElement.innerHtml = s;
    > contentDocument.body.appendChild(newScriptElement) ;



    try
    setTimeout( DoIt );

    That way the jsengine gets a chance to compile your code before you call it.

  3. Re: Inserting a script element and executing

    On Mon, Mar 31, 2008 at 2:29 PM, John J Barton
    wrote:
    > > var s = 'function DoIt(){alert("hello");}'
    > > var contentDocument = window._content.document;
    > > newScriptElement = contentDocument.createElement('script');
    > > newScriptElement.setAttribute('id', 'MY_ID');
    > > newScriptElement.setAttribute('type', 'text/javascript');
    > > newScriptElement.innerHtml = s;
    > > contentDocument.body.appendChild(newScriptElement) ;

    >
    > try
    > setTimeout( DoIt );
    >
    > That way the jsengine gets a chance to compile your code before you call it.


    I don't understand -- the JS engine always has to compile the code
    before calling it, and the