Thanks Rob,
Actually, I've got the method you suggest working well in another area
of my code. The problem is when I load HTML content dynamically using
XMLHTTP -- how do I get the script added when it is received as
"responseText" from the XMLHTTP request?
Perhaps I should parse the responseText and create new function
objects?
Hmm... tricky?
Steve
RobG wrote:[color=blue]
>
Join Bytes! wrote:[color=green]
> > Using XMLHTTP and DOM I'm able to load new HTML page content.
> >
> > I'd now like to load small snippets of javascript with the HTML markup
> > and have that <script> incorporated into the page. If any of the loaded
> > script exists outside a function definition (eg: a call to a function),
> > I'd like that code to be executed as soon as its added to the DOM.
> >
> > Can anyone suggest the best way to do this? I've Googled but not found
> > anything comprehensive. Do I need to use the eval() method or is there
> > a better way?
> >
> > Thanks,
> >
> > Steve
> >[/color]
>
> The best way is to add a script element with a src attribute:
>
> var oS = document.createElement('script');
> oS.type = 'text/javascript';
> oS.src = 'someLink/cmd.js';
>
> <URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/f39a6a56185a49c1/a4c3d5dcce0a08e6?tvc=1&q=document.createElement(%27script%27)&hl=en#a4c3d5dcce0a08e6>
>
> But not all browsers may support the above. Also be careful of calling
> functions in any linked file as they must be downloaded & added to the
> document before they are ready to use. Adding a script element and then
> depending on its content being immediately available is risky.
>
> Any statements outside functions will be executed when the script is loaded.
>
> AFAIK, you can't modify the content of a script element dynamically.
> You can clone a script element that has statements outside functions and
> add it to some part of the document, but the statements will not be
> executed (at least not in Firefox or IE).
>
>
> --
> Rob[/color]