add javascript on the fly

O

ojvm

Hi every body. as my post says, i want to add javascript to my page on
the fly. for example

page1.html

<html>
<head>
<script type='text/javascript'src='controlador.js'></script>

<title>...</title>
</head>
<body>
......
</body>
</html>

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

page1.html

<html>
<head>
<script type='text/javascript'src='controlador.js'></script>
<script type='text/javascript'src='new-file-just-added.js'></script>

<title>...</title>
</head>
<body>
......
</body>
</html>

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.

hope you can helpme. thanks
 
B

Bart Van der Donck

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

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.

hope you can helpme. thanks

var sc = document.createElement('script')
sc.type = 'text/javascript'
sc.src = 'new-file-just-added.js'
document.getElementsByTagName('head')[0].appendChild(sc)

Before accessing the content of of file.js, you should first make sure
that it's fully loaded.

http://groups.google.com/group/comp.lang.javascript/msg/497fc1b1393136b9

Hope this helps,
 
R

RobG

Hi every body. as my post says, i want to add javascript to my page on
the fly. for example

<FAQENTRY>

This question seems to have been asked about once a week for the past
year - time for an FAQ entry?

</FAQENTRY>
 
I

Ivan Marsh

Hi every body. as my post says, i want to add javascript to my page on
the fly. for example

Server-side includes happen at the time the page is downloaded... so, you
can't do that.

Look into AJAX.
 
R

Richard Cornford

Randy Webb wrote:
Question:
How do I dynamically load a script block?
<snip>

It might be better if the question was along the lines of "How can a
script element/file be dynamically loaded into an already loaded web
page" so that the whole question of using - document.wirte - can be
excluded from the answer (to keep it simple enough for a quick answer
(and preclude some of the quibbling up front).

Richard.
 
B

Bart Van der Donck

Randy said:
[...]
Question:
How do I dynamically load a script block?

In 95% of the cases, one should be fine to design the application so
that it always loads the external resource; and then decide to
(whether or not) execute code function-wise. Objections regarding
memory use are likely to sprout from the remaining 5%.
 
I

Ivan Marsh

Ivan Marsh said the following on 3/12/2007 6:42 PM:

Who said anything about Server-side includes?

This is the javascript equivalent of a server-side include: <script
type='text/javascript'src='controlador.js'> said:
I bet I can :)

Not without reloading the page you can't.
AJAX is the *worst* way to try to dynamically load a .js file on the
fly.

I wasn't suggesting loading a .js file with AJAX... it is a good way to
present asynchronous changes to the page however.
 
O

ojvm

Tank you bart, your solution worked really fine, is exactly what a was
looking for. only as a comment. before i post my question, i looked
for in many places, but i couldn't find any thing.


thanks again.
 
B

Bart Van der Donck

ojvm said:

Russian? German?
you bart, your solution worked really fine, is exactly
what a was looking for. only as a comment. before i post
my question, i looked for in many places, but i couldn't
find any thing.

You're welcome.
 
I

Ivan Marsh

Ivan Marsh said the following on 3/13/2007 10:17 AM:

No it isn't, but ok.
Equivalent.


Are you a gambling man?

You should search the archives for loadJSFile and my name before
answering that question above. As I know, without a doubt, that I can
load a .js file after the page has loaded *without* reloading the page.

Loading a .js file is nothing.

Do it by dropping a second <script
type='text/javascript'src='controlador.js'></script> line on to the
page... it cannot be done without reloading the page.

Sorry if it was not clear what I was talking about... which was obviously
a not terribly clear explanation as to why the example given wouldn't
work.
 
E

Evertjan.

Randy Webb wrote on 13 mrt 2007 in comp.lang.javascript:
<proposal>
How can a script element/file be dynamically loaded after the page has
finished loading?
</proposal>

.... be loaded and executed ...

Loading with AJAX to be shown as text,
does not seem the point,
and such loading as text and then simple eval()-ing
seems too evil to contemplate.
 
B

Bart Van der Donck

Randy said:
Bart Van der Donck said the following on 3/13/2007 4:49 AM:
Randy Webb wrote:
[...]
Question:
How do I dynamically load a script block?
In 95% of the cases, one should be fine to design the application so
that it always loads the external resource; and then decide to
(whether or not) execute code function-wise. Objections regarding
memory use are likely to sprout from the remaining 5%.

I beg to differ. One of the internal applications I maintain uses .js
files as the data transfer method. The possibilities from the main page
number somewhere around 20,000 .js files (I could login to work and
check but can't right now). Are you suggesting that instead of loading
them on the fly I should load all 20,000 of those .js files when the
page loads? It would take days to open the app.

You're right of course. This is definitely such a case where one
should load code partially/dynamically.
 
B

Bart Van der Donck

ojvm said:
in fact mexican. yes i know, i have a lot of misspellings.

It looks like my joke of the Russian/German tank didn't make it over
the ocean :)
 
A

ASM

Ivan Marsh a écrit :
Not without reloading the page you can't.

I think you can

<html>
<script type="text/javascript">
function loadJS(file) {
var s = document.createElement('SCRIPT');
s.type = 'text/javascript';
s.src = file;
document.body.appendChild(s);
}
</script>
<a href="#" onclick="loadJS('test.js'); return false;">
say hello
</a>
</html>


file 'test.js' :
alert('hello');


tested with FF 2, Safari 1.3, Opera 9, iCab 3

don't know what that gives with IE ...
 
A

ASM

Randy Webb a écrit :
You should search the archives for loadJSFile and my name before
answering that question above. As I know, without a doubt, that I can
load a .js file after the page has loaded *without* reloading the page.

If it is what I think,
can you give me back the address of this test page ?
I'm unable to find it.
 
A

ASM

Ivan Marsh a écrit :
Do it by dropping a second <script
type='text/javascript'src='controlador.js'></script> line on to the
page... it cannot be done without reloading the page.

with DOM it is possible.

But ... possibly IE can't understand that ?
 
A

ASM

Randy Webb a écrit :
ASM said the following on 3/13/2007 2:44 PM:

IE can't understand what part?

It is so mysterious ... how to know ? :)
IE is easier to load a script,
dynamically, than any other browser:
<script type="text/javascript">
document.getElementById('myScript').src="file2.js";
</script>

I don't recall for sure but I believe IE even handles it as a plain
myScript.src instead of the gEBI call.

it would be not very surprising (with all approximations it accepts)
 
R

RobG

Randy Webb wrote on 13 mrt 2007 in comp.lang.javascript:


... be loaded and executed ...

Loading with AJAX to be shown as text,
does not seem the point,
and such loading as text and then simple eval()-ing
seems too evil to contemplate.

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.

It causes quite a few questions related to the change of scope for
eval'd script.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top