By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,043 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,043 IT Pros & Developers. It's quick & easy.

Running Dynamicaly loaded JavaScript

P: n/a
Hi all,

In an AJAX environment, is it possible to load and display some HTML
and JavaScript and have that JavaScript functional?

I have tried but the JavaScript functions dont appear to be visible -
Im assuming this is becasue they were not loaded when the initial AJAX
application/page originally loaded.

Conceptual example if it helps:
Your viewing a page of content
You click on a button to get a calculator
AJAX calls the calculator HTML and Javascript from the server and
displays it
You can then use use the calculator

When I try this, I see the HTML calculator but the butons complain the
the Javascript functions dont exist.
--
http://creativeobjectworld.com - (cow)
a free-form, text-based, browser-based, real-time, multi-user virtual
world.

Oct 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
aum
On Mon, 09 Oct 2006 23:43:36 -0700, wolis wrote:
Hi all,

In an AJAX environment, is it possible to load and display some HTML
and JavaScript and have that JavaScript functional?

I have tried but the JavaScript functions dont appear to be visible -
Im assuming this is becasue they were not loaded when the initial AJAX
application/page originally loaded.

Conceptual example if it helps:
Your viewing a page of content
You click on a button to get a calculator
AJAX calls the calculator HTML and Javascript from the server and
displays it
You can then use use the calculator

When I try this, I see the HTML calculator but the butons complain the
the Javascript functions dont exist.
Workaround - separate the js and the html into separate strings, call
these 'jsCode' and 'htmlCode'.
Then, add the js into the doc with something like:

var js = document.createElement("script");
js.setAttribute("language", "Javascript");
js.setAttribute("style", "text/javascript");
js.textContent = jsCode;
document.body.appendChild(js);

Then, add the html into the doc with something like:

var d = document.createElement("div");
d.innerHTML = htmlCode;
document.body.appendChild(d);

After creating the 'script' element, the act of adding it to document.body
will cause the code to execute, so your functions should be defined.

Cheers
aum

Oct 10 '06 #2

P: n/a

aum wrote:
On Mon, 09 Oct 2006 23:43:36 -0700, wolis wrote:
>
Workaround - separate the js and the html into separate strings, call
these 'jsCode' and 'htmlCode'.
Then, add the js into the doc with something like:

var js = document.createElement("script");
js.setAttribute("language", "Javascript");
js.setAttribute("style", "text/javascript");
js.textContent = jsCode;
document.body.appendChild(js);

Then, add the html into the doc with something like:

var d = document.createElement("div");
d.innerHTML = htmlCode;
document.body.appendChild(d);

After creating the 'script' element, the act of adding it to document.body
will cause the code to execute, so your functions should be defined.

Cheers
aum
Ah, wonderful.. thanks aum.

Im assuming simply replacing the content of an existing script element
would not trigger the code to execute?

Ill have a play, thanks again.
--
http://creativeobjectworld.com - (cow)
a free-form, text-based, browser-based, real-time, multi-user virtual
world.

Oct 10 '06 #3

P: n/a
aum
On Tue, 10 Oct 2006 01:12:39 -0700, wolis wrote:
Ah, wonderful.. thanks aum.
Hope it helps
Im assuming simply replacing the content of an existing script element
would not trigger the code to execute?
Javascript, and the way browsers handle it, are very often
counter-intuitive and can only be determined by trial and error, with much
gnashing of teeth.
Ill have a play, thanks again.

Oct 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.