Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

add javascript on the fly


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

ojvm wrote:
> 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,

--
 Bart

On Mar 13, 5:20 am, "ojvm" <yomerosoy2005-foros@yahoo.com> wrote:

> 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>

--
Rob

On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:
> 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.

RobG said the following on 3/12/2007 4:30 PM:

> On Mar 13, 5:20 am, "ojvm" <yomerosoy2005-foros@yahoo.com> wrote:
>> Hi every body. as my post says, i want to add javascript to my page on
>> the fly. for example

> <F******Y>

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

> </F******Y>

And people called me, ummm, what was it, eh, ahh yes, a "true
humanitarian" (Peter Michaux) in the createTextNode and IE7 thread that
I think has become more referred to in the last 6 months than any other
thread in the archives:

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_thread/thr...>

If an entry is made, it should also cover the AJAX side of it where it
more of a "How do I get my inserted scripts to execute?" Or, should it
be two different entries?

Question:
How do I dynamically load a script block?

Answer:
Ummmm.... :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

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

> On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:

>> 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...

Who said anything about Server-side includes?

> so, you can't do that.

I bet I can :)

> Look into AJAX.

AJAX is the *worst* way to try to dynamically load a .js file on the fly.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Randy Webb wrote:

<snip>
> 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.

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%.

--
 Bart

On Mon, 12 Mar 2007 19:25:51 -0400, Randy Webb wrote:
> Ivan Marsh said the following on 3/12/2007 6:42 PM:
>> On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:

>>> 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...

> Who said anything about Server-side includes?

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

>> so, you can't do that.

> I bet I can :)

Not without reloading the page you can't.

>> Look into AJAX.

> 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.
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.

ojvm wrote:
> Tank

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.

--
 Bart

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

> On Mon, 12 Mar 2007 19:25:51 -0400, Randy Webb wrote:

>> Ivan Marsh said the following on 3/12/2007 6:42 PM:
>>> On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:

>>>> 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...
>> Who said anything about Server-side includes?

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

No it isn't, but ok.

>>> so, you can't do that.
>> I bet I can :)

> Not without reloading the page you can't.

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Equivalent.

>>>> so, you can't do that.
>>> I bet I can :)

>> Not without reloading the page you can't.

> 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.

Ivan Marsh said the following on 3/13/2007 12:34 PM:

> On Tue, 13 Mar 2007 12:22:01 -0400, Randy Webb wrote:

>> Ivan Marsh said the following on 3/13/2007 10:17 AM:
>>> On Mon, 12 Mar 2007 19:25:51 -0400, Randy Webb wrote:

>>>> Ivan Marsh said the following on 3/12/2007 6:42 PM:
>>>>> On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:

<snip>

>>>>> so, you can't do that.
>>>> I bet I can :)
>>> Not without reloading the page you can't.
>> 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.

After the page has finished loading. Which was the original request.

<quote cite="original post">
now when the page has been loaded, acording some user's actions, i'd
like to add a new script, like this.
</quote>

loadJSFile (in it's many incarnations) does precisely what the OP asked for.

> 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.

That doesn't do what was asked though. Although I can do that without
reloading the page. It just won't get executed with a few exceptions.

> 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.

Yes, we were comparing two different things. I was referring to what the
OP asked about (which is almost trivial to be honest). And I am not sure
what you are referring to, other than trying to add it on a page load,
before the page has finished loading.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

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.

Dynamically loading .js files is a much better alternative (to date
anyway) than XHR is is the reason I use it internally at work.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Richard Cornford said the following on 3/12/2007 8:35 PM:

> Randy Webb wrote:
> <snip>
>> 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).

<proposal>
How can a script element/file be dynamically loaded after the page has
finished loading?
</proposal>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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

--
 Bart

On 13 mar, 09:49, "Bart Van der Donck" <b@nijlen.com> wrote:

> ojvm wrote:
> > Tank

> Russian? German?

in fact mexican. yes i know, i have a lot of misspellings. thank you
again.

ojvm wrote:
> On 13 mar, 09:49, "Bart Van der Donck" <b@nijlen.com> wrote:

> > ojvm wrote:
> > > Tank

> > Russian? German?

> 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 :)

--
 Bart

Ivan Marsh a crit :

> On Mon, 12 Mar 2007 19:25:51 -0400, Randy Webb wrote:

>> Ivan Marsh said the following on 3/12/2007 6:42 PM:
>>> On Mon, 12 Mar 2007 12:20:07 -0700, ojvm wrote:

>>> so, you can't do that.
>> I bet I can :)

> 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 ...

--
Stephane Moriaux et son (moins) vieux Mac dj dpass

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.

--
Stephane Moriaux et son (moins) vieux Mac dj dpass

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 ?

--
Stephane Moriaux et son (moins) vieux Mac dj dpass
Stephane Moriaux and his (less) old Mac already out of date

ASM said the following on 3/13/2007 2:38 PM:

It will, without a doubt, give the alert.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

ASM said the following on 3/13/2007 2:42 PM:

> 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.

<URL: http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>

That one?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc