add javascript on the fly

E

Evertjan.

RobG wrote on 14 mrt 2007 in comp.lang.javascript:
Never-the-less, it is a common practice to return HTML, strip the
script elements, add the result to the page using innerHTML and eval
the content of the script elements.

Common?

Eval-utionarily evel perhaps?
It causes quite a few questions related to the change of scope for
eval'd script.

Indeed.
 
B

Bart Lateur

ojvm said:
now when the page has been loaded, acording some user's actions, i'd
like to add a new script, like this. ...
right now i'm doing this.

document.write("<script type='text/javascript'src='new-file-just-
added.js''></script>");

obviusly this is not working properly, because, i think, in that way
all the content is erased.

Don't use document.write. Use document.createElement('script'), set the
src, and then do appendChild. You can do it in the head, like this:

head = document.getElementsByTagName('head')[0];
head.appendChild(script);

but you can also add it to the body, which is easier:

document.body.appendChild(script);
 
R

Richard Cornford

RobG said:
Never-the-less, it is a common practice to return HTML,
strip the script elements, add the result to the page
using innerHTML and eval the content of the script elements.

Has "common practice" ever been a good justification for anything when it
comes to browser scripting? After all it is common practice to expose
visitors to web site scripts that spit out javascript errors at the
slightest provocation, so common that even organisations a big (and
presumably well-funded) as google do it all the time.
It causes quite a few questions related to the change of
scope for eval'd script.

Yes, and often form people who want to be told how to dig themselves out
of the hole they have gotten themselves into.

Richard.
 
R

RobG

Has "common practice" ever been a good justification for anything when it
comes to browser scripting?

I didn't infer support or approval for the technique, I just noted the
fact that its use is reasonably common. Many AJAX libraries include a
method to eval script elements returned in HTML fragments, which is
not so subtle encouragement to do so.

If it is worth noting as an issue, perhaps the FAQ entry should say
something about it?
 
B

Bart Van der Donck

Randy said:
RobG said the following on 3/14/2007 7:17 PM:

That, to me, is just one more strike against the many
libraries. And another reason I am glad I don't use AJAX :)

Same here. I do add some ajaxy-looking tricks here and there (no need
to look like a 1995 CGI), but I have always been reluctant to lean too
heavily on client scripts for fundamental issues.

I would even state that this also applies to javascript as a whole -
it's never played a crucial role in my development strategies. The
technology at the server side is just too long-proven and too good,
mainly because of a higher level of efficiency, reliability,
conceptuality, tradition and more possibilities. I realize that this
view may be somewhat coloured by personal preferences and experiences.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top