473,699 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 12 '07 #1
34 9659
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.create Element('script ')
sc.type = 'text/javascript'
sc.src = 'new-file-just-added.js'
document.getEle mentsByTagName( '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....7fc1b1393136b9

Hope this helps,

--
Bart

Mar 12 '07 #2
On Mar 13, 5:20 am, "ojvm" <yomerosoy200 5-foros...@yahoo. comwrote:
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

Mar 12 '07 #3
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.
Mar 12 '07 #4
RobG said the following on 3/12/2007 4:30 PM:
On Mar 13, 5:20 am, "ojvm" <yomerosoy200 5-foros...@yahoo. comwrote:
>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.c om/group/comp.lang.javas cript/browse_thread/thread/7e23f42490c301d e/56d47ba8d4d73e3 0?lnk=gst&q=cre atetextnode+IE7 &rnum=1#56d47ba 8d4d73e30>

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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 12 '07 #5
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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 12 '07 #6
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.

Mar 13 '07 #7
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

Mar 13 '07 #8
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.
Mar 13 '07 #9
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.
Mar 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
8689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8885
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.