473,396 Members | 1,766 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,396 software developers and data experts.

How to add javascript code dynmically?

Hello,

I need to add some javascript code block dynamically to a web page.
I looked into the various postings at various groups but none seems to
be solving my problem.

Among the approaches suggested first one is to create a script element
and set its properties (src etc) and then adding this script element to
the head element.
This works good for a dynamically including the files. But, in my case
I do not have any files but generating the content dynamically which
should be available to other javascript functions in the page. The
script that I need to add dynmically is given below :
------------------------------------------------------------
var numberOfReowsDisplayed =
eval(formFieldsArray[getNetuiTagName("numberOfRecordsDisplayed",this)]).value

if(netui_names && numberOfReowsDisplayed && numberOfReowsDisplayed > 0
){
s = ''
for(i=0; i<numberOfReowsDisplayed; i++){
s = s +
"netui_names.startDate["+i+"]={actionForm.serviceProductAssociation[" +
i + "].startDate}" + "\n"
}

}
------------------------------------------------------------
Content of the variable 's' needs to be added dynamically.
I cant do this at the server side as the formFieldsArray is generateed
by the netUi framework, control of which I dont have, hence I cant
decide if I should output when I am on the server side.
I am crossing deadline and tired of various trial and errors.

Any help is greatly appreciated.

Thanks a ton,
cg

Oct 26 '05 #1
4 1779
VK
> ...
Content of the variable 's' needs to be added dynamically.


To where? I mean do you need i) an "s" block conditionally executed in
the main script stream or ii) you need to conditionally extend with "s"
an existing function/object or iii) you need to conditionally create
new "s"-based function/object?

Oct 26 '05 #2
Thanks VK, for your quick response.

1) . "s" is a variable that I have bulit conditionally which contains
additional elements of an array already rendered as a global variable
by the server side netui framework. For example, the server side
framework outputs the below block

//---------------------------------------------------------------------------------------------------
// Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
var netui_names = new Object();
netui_names.action="{actionForm.action}"
netui_names.numberOfRecordsDisplayed="{actionForm. numberOfRecordsDisplayed}"
netui_names.startDate="{actionForm.serviceProductA ssociation[9].startDate}"
netui_names.productCategory="wlw-select_key:{actionForm.productCategory}"
netui_names.productSeries="{actionForm.productSeri es}"
netui_names.productLine="{actionForm.productLine}"
netui_names.productDescription="{actionForm.produc tDescription}"
netui_names.newServiceProductForm="newServiceProdu ctForm"
netui_names.isAssociated="wlw-checkbox_key:{actionForm.serviceProductAssociation[9].isAssociated}"
netui_names.programs="wlw-select_key:{actionForm.programId}"
netui_names.productGroup="{actionForm.productGroup }"
netui_names.productNumber="{actionForm.productNumb er}"
netui_names.serviceProducts="wlw-select_key:{actionForm.spId}"
netui_names.endDate="{actionForm.serviceProductAss ociation[9].endDate}"
netui_names.currentPage="{actionForm.currentPage}"
//---------------------------------------------------------------------------------------------------

I need to extend the contenet of the array by adding the below elements
(this will be the content of variable 's' )

//-----------------------------------------------------------------------------------------------------------------------
netui_names.startDate[0]={actionForm.serviceProductAssociation[0].startDate}
netui_names.startDate[1]={actionForm.serviceProductAssociation[1].startDate}
netui_names.startDate[2]={actionForm.serviceProductAssociation[2].startDate}
netui_names.startDate[3]={actionForm.serviceProductAssociation[3].startDate}
netui_names.startDate[4]={actionForm.serviceProductAssociation[4].startDate}
netui_names.startDate[5]={actionForm.serviceProductAssociation[5].startDate}
netui_names.startDate[6]={actionForm.serviceProductAssociation[6].startDate}
netui_names.startDate[7]={actionForm.serviceProductAssociation[7].startDate}
netui_names.startDate[8]={actionForm.serviceProductAssociation[8].startDate}
netui_names.startDate[9]={actionForm.serviceProductAssociation[9].startDate}
//-----------------------------------------------------------------------------------------------------------------------

2) . I need to add s to the webpage so that it is accessible globally
in javascript ( in onclick functions etc) .

Please suggest.

Thanks and Regards,
CG

Oct 26 '05 #3

cotton_gear wrote:
Hello,

I need to add some javascript code block dynamically to a web page.
I looked into the various postings at various groups but none seems to
be solving my problem.

Among the approaches suggested first one is to create a script element
and set its properties (src etc) and then adding this script element to
the head element.
This works good for a dynamically including the files. But, in my case
I do not have any files but generating the content dynamically which
should be available to other javascript functions in the page. The
script that I need to add dynmically is given below :
------------------------------------------------------------
var numberOfReowsDisplayed =
eval(formFieldsArray[getNetuiTagName("numberOfRecordsDisplayed",this)]).value

if(netui_names && numberOfReowsDisplayed && numberOfReowsDisplayed > 0
){
s = ''
for(i=0; i<numberOfReowsDisplayed; i++){
s = s +
"netui_names.startDate["+i+"]={actionForm.serviceProductAssociation[" +
i + "].startDate}" + "\n"
}

}
------------------------------------------------------------
Content of the variable 's' needs to be added dynamically.
I cant do this at the server side as the formFieldsArray is generateed
by the netUi framework, control of which I dont have, hence I cant
decide if I should output when I am on the server side.
I am crossing deadline and tired of various trial and errors.

Any help is greatly appreciated.

Thanks a ton,
cg
Some thoughts:-
[snip] s = ''
1. Use "var" to declare the variable, unless you want it to be global.
[snip] "netui_names.startDate["+i+"]={actionForm.serviceProductAssociation[" +
i + "].startDate}" + "\n"


2. I think your code contains a slight error:-

The "{" and "}" characters do not need to be used here, and are invalid
in this grammar context.


3. Why do you need to add "s" dynamically?

Why not:

for(i=0; i<numberOfReowsDisplayed; i++)
{

netui_names.startDate["+i+"]=actionForm.serviceProductAssociation["+i
+"].startDate;
}
4. If you need to add "s" dynamically, then the roughest way is to use
"eval". Although, always try to use eval only where no other solution
is available.

I.e.

eval(s);

Julian

Oct 26 '05 #4
Hi Julian,

Thanks for all the info, eval(s) is making things work.

Just to answer your qustions :
1. I will definitly follow your suggestions
2. Code contained error as I removed some part and pasted, so missed
out some part.
3. I need to add the content of 's' dynamically because as I have to
extend the arrays generated by the server side framework.
4. eval(s) has solved my proble.
Thanks again for your timely help.
CG

Oct 27 '05 #5

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

Similar topics

2
by: _MC_ | last post by:
Hi, i want to structure two elements (each conists of 1 Label and 1 Textbox) in an Table. As I use an Content Place Holder, i thought it is possible to add the table via Controls.add(Literal)....
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
Frinavale
by: Frinavale | last post by:
JavaScript in ASP.NET Using JavaScript in ASP.NET pages can make your application seem to work faster and prevent unnecessary calls to the server. JavaScript can be used to perform client-side...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.