473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Dynamic insertion and execution of JavaScript

Hi,
I'm developing a webpage that is composed by several divs. These divs
are supplied by the server depending on the user that made the request.
Some of these divs require some javascript functions to be called when
they are added to the page. I add the content using innerHTML (function
renderPage(data) { document.getElementsByTagName('body')[0].innerHTML =
data; }).
(data:
<div id="id" style="....">
.....
<script type="text/javascript">
initDiv();
</script>
....
</div>

The div is displayed in the page with no problems except for the
javascript functions that is not executed. I know that innerHTML just
replaces the code and do not execute any javascript in it.
Is there any possible way to do this?

thanks,
José Pedro Tavares

Apr 20 '06 #1
3 3706
za*****@gmail.com wrote:
I'm developing a webpage that is composed by several divs. These divs
are supplied by the server depending on the user that made the request.
Some of these divs require some javascript functions to be called when
they are added to the page. I add the content using innerHTML (function
renderPage(data) { document.getElementsByTagName('body')[0].innerHTML =
data; }).
(data:
<div id="id" style="....">
....
<script type="text/javascript">
initDiv();
</script>
...
</div>

The div is displayed in the page with no problems except for the
javascript functions that is not executed. I know that innerHTML just
replaces the code and do not execute any javascript in it.
Is there any possible way to do this?


For cross-browser scripting, you would need to attach information to `data'
about the code to be executed before or after you added the content. For
example:

function initDiv()
{
// ...
}

function renderPage(data)
{
if (typeof data.before == "function") data.before();

// probably you want `+=' here instead
document.body.innerHTML = data.content;

if (typeof data.after == "function") data.after();
}

renderPage({
content: '<div id="id" style="...">...</div>',
after: initDiv
});

Probably it would be best if renderPage() was a method of a user-defined
object. And maybe your `div' element does not need an ID.
PointedEars
Apr 20 '06 #2
Hi and thanks for the fast reply.
I've tried the solution with few success, although the problem may be
in the architecture in use. We are using DWR that generates javascript
functions from Java objects allowing the browser to do remote calls on
Java objects. The call to the renderPage function is generated from one
of these classes and when the function is called both content and after
arguments are of type string. Is there any way for me to force the
after argument to be of type function? (parse of the string and
construction of a script object maybe?

thanks,
José Pedro Tavares

Apr 21 '06 #3
Well I got it sort of working using Function objects. I have another
problem though. The function is not executed properly because it seems
the includes needed for the function to execute are not interpreted. Do
anyone has a similar problem or knows how can I solve it?

thanks,
José Pedro Tavares

Apr 21 '06 #4

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

Similar topics

3
by: Tormod Omholt-Jensen | last post by:
I need to dynamically insert the following applet code in my document: <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = 0 HEIGHT = 0 NAME = "myApplet"...
2
by: Randell D. | last post by:
HELP! Its taken me ages - I'm a newbie and I've compiled bits of code that I've read in this newsgroup over time to create one of my most intricate functions to date... Basically, the script...
8
by: S.Hoitinga | last post by:
Hi, I am trying to create a header on a page that switches between 7 textitems in an array. using this code: <script language="JavaScript"> season=new...
2
by: thinkfr33ly | last post by:
I have a page that inserts a block of javascript dynamically into the page "Test.html". The inserted block, "Block A", then does a document.write of another script block "Block B". This script...
1
by: tribal boy | last post by:
Guys, I am using a dynamic menu which uses xml,xsl a css file and javascript. This works fine when there are no server controls around or underneath it. The problem is whenever the menu...
28
by: Peter Michaux | last post by:
Hi, I'm playing with dynamic script insertion to make a request to the server for a JavaScript file to be automatically run when it arrives in the browser. It works but... The page caching...
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
1
by: vikramcbz | last post by:
i want java programs where if any insertion happens in mysql database it should run the java program
3
by: aj | last post by:
DB2 LUW 8.1 fixpak 14 Red Hat EL AS 4.4 I'm trying to diagnose some nocturnal CPU pressure, and am trying to understand the dynamic statement cache as it applies to LUW. The only doc/redbooks...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.