473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentsByTagName( '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 3714
za*****@gmail.c om 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.getEle mentsByTagName( '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.i nnerHTML = 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
13412
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" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_3_1_03-win.cab#Version=1,3,1,3"> <PARAM NAME = CODE VALUE = "FooApplet.class" > <PARAM NAME = CODEBASE VALUE = "http://foo.com/java/" > <PARAM NAME = ARCHIVE VALUE = foo.jar" > <PARAM NAME = NAME VALUE =...
2
5368
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 gets called with a single arguement (full path to an image). The image is supposed to be downloaded to the cache, and when complete, a new window opened that is slightly larger insize then the images dimensions... The new window will contain the...
8
1953
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 Array("summer","winter","fall","spring",") i=0 function raise()
2
2059
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 block does its own document.write of one last script block "Block C". The way this occurs is somewhat out of my control, and I need to find a way to work within this framework. This only needs to work in IE 5.5 or better. My problem is that the...
1
4302
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 encounters a server control, it closes itself.
28
3453
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 is too good. When I revisit the page or refresh the page, and then redo the script insertion, the browser doesn't even hit the server to check for a newer version of the JavaScript file. The same old script runs with each insertion.
7
3028
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 proper tier to put it since is a data function But then I've heard that writing SQL in my client .NET code might run just as fast? Dynamic SQL or something? And then there's LINQ on the horizon. Is it a successor to everything
1
1279
by: vikramcbz | last post by:
i want java programs where if any insertion happens in mysql database it should run the java program
3
5728
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 I am finding are for Z/OS, which I am completely ignorant of. I am using only Java and JDBC in my applications. No static SQL.
0
9645
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
9480
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
10327
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
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
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.