Passing varray elements(from JSP) to an array in javascript

Dear All,

I am supposed to display a menu which has sub - menus. The no of menus and sub - menus have to decided at run - time i.e. the entire data has to be populated from a table in a database.

One way of doing it can be getting the data into a varray from the table and then passing it to an array in javascript. Now, is it possible to pass a varray (from a JSP) to an array in javascript.

If this is possible then i need the code as early as possible.It's on an urgent basis.

Plz help


Nikhil Bansal
Who is Participating?
ZvonkoConnect With a Mentor Systems architectCommented:
Again, I got running that  example on my PC, so all I can say is that you have to dynamicaly create this file content:

But beside that menu structure script you need some driver scripts and some gifs in defined directory positions.

I hope you do not understand this question like "how to crack a menu product", or do you still ask how to paste html code with JavaScript content into your page?
You have to produce this script dynamicaly on your page to get upper example3.html running:
//----------DHTML Menu Created using AllWebMenus PRO ver 3.1-#514---------------
var awmMenuName='example3';
var awmLibraryPath='/../awmData-menu';
var awmImagesPath='/example_images';
var awmSupported=(navigator.appName + navigator.appVersion.substring(0,1)=="Netscape5" || document.all || document.layers || navigator.userAgent.indexOf('Opera')>-1)?1:0;
if (awmAltUrl!='' && !awmSupported) window.location.replace(awmAltUrl);
if (awmSupported){
var awmMenuPath;
if (document.all) mpi=document.all['awmMenuPathImg-example3'].src;
if (document.layers) mpi=document.images['awmMenuPathImg-example3'].src;
if (navigator.appName + navigator.appVersion.substring(0,1)=="Netscape5" || navigator.userAgent.indexOf('Opera')>-1) mpi=document.getElementById('awmMenuPathImg-example3').src;
while ("'")>-1) {awmMenuPath=awmMenuPath.replace("'", "&#39;");}
var nua=navigator.userAgent,scriptNo=(nua.indexOf('Safari')>-1)?7:(nua.indexOf('Gecko')>-1)?2:((document.layers)?3:((nua.indexOf('Opera')>-1)?4:((nua.indexOf('Mac')>-1)?5:((nua.indexOf('Konqueror')>-1)?6:1))));
prompt("<SCRIPT SRC='",awmMenuPath+awmLibraryPath+"/awmlib"+scriptNo+".js");
document.write("<SCRIPT SRC='"+awmMenuPath+awmLibraryPath+"/awmlib"+scriptNo+".js'><\/SCRIPT>");
var n=null;

var awmSubmenusFrame='';
var awmSubmenusFrameOffset;
var awmOptimize=0;
function awmBuildMenu(){
if (awmSupported){
awmCreateCSS(1,2,1,'#FFFFFF','#000000',n,'14px sans-serif',n,'none',2,n,5,4);
awmCreateCSS(1,2,1,'#000000','#FFFFFF',n,'14px sans-serif',n,'none',1,n,0,1);
awmCreateCSS(0,2,1,'#FFFFFF','#AFAFAF',n,'14px sans-serif',n,'none',1,n,0,1);
awmCreateCSS(0,2,1,'#FFFFFF','#3F3F3F',n,'14px sans-serif',n,'none',1,n,0,1);
awmCreateCSS(1,2,1,'#FFFFFF','#000000',n,'14px sans-serif',n,'none',2,n,0,4);
awmCreateCSS(1,2,1,'#000000','#FFFFFF',n,'14px sans-serif',n,'none',1,n,2,1);
awmCreateCSS(0,2,1,'#FFFFFF','#AFAFAF',n,'14px sans-serif',n,'none',1,n,2,1);
awmCreateCSS(0,2,1,'#FFFFFF','#3F3F3F',n,'14px sans-serif',n,'none',1,n,2,1);
awmCreateCSS(1,2,0,'#FFFFFF','#000000',n,'14px sans-serif',n,'none',2,n,0,4);
awmCreateCSS(1,2,2,'#000000','#FFFFFF',n,'14px sans-serif',n,'none',1,n,4,1);
awmCreateCSS(0,2,2,'#FFFFFF','#AFAFAF',n,'14px sans-serif',n,'none',1,n,4,1);
awmCreateCSS(0,2,2,'#FFFFFF','#3F3F3F',n,'14px sans-serif',n,'none',1,n,4,1);
awmCreateCSS(1,2,1,'#000000','#FFFFFF',n,'14px sans-serif',n,'none',1,n,4,1);
awmCreateCSS(0,2,1,'#FFFFFF','#AFAFAF',n,'14px sans-serif',n,'none',1,n,4,1);
awmCreateCSS(0,2,1,'#FFFFFF','#3F3F3F',n,'14px sans-serif',n,'none',1,n,4,1);
var s0=awmCreateMenu(0,0,0,0,1,0,1,0,0,30,30,1,1,1,0,"&nbsp Horizontal menu &nbsp","",n,1,1,0,1,n,n,100);
it=s0.addItem(2,3,4,"Infinite<br>&nbsp submenus &nbsp",n,n,"","",n,n,n,"","new");
var s1=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"&nbsp Submenu1 &nbsp","",n,1,1,1,0,n,n,100);
var s2=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"&nbsp Submenu2 &nbsp","",n,1,1,1,0,n,n,100);
var s3=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"&nbsp Submenu3 &nbsp","",n,1,1,1,0,n,n,100);
var s4=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"&nbsp Submenu4 &nbsp","",n,1,1,1,0,n,n,100);
var s5=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"&nbsp Unlimited submenus...","",n,1,1,1,0,n,n,100);
it=s5.addItem(2,3,4,"&nbsp;&nbsp;...and i could still go on!&nbsp;&nbsp;",n,n,"","",n,n,n,n,n);
it=s0.addItem(2,3,4,"Vertical<br>&nbsp submenus &nbsp",n,n,"","",n,n,n,"","new");
var s1=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"Vertical Submenu","",n,1,1,1,0,n,n,100);
it=s1.addItemWithImages(7,8,9,"&nbsp;with submenu title&nbsp;",n,n,"",n,n,n,0,0,0,0,1,0,"",n,n,n,n,n);
var s2=it.addSubmenu(0,0,-4,2,0,0,0,6,5,"Title","",n,1,1,1,0,n,n,100);
it=s1.addItemWithImages(7,8,9,"&nbsp;or without submenu title&nbsp;",n,n,"",n,n,n,0,0,0,0,1,0,"",n,n,n,n,n);
var s2=it.addSubmenu(0,0,-4,2,0,0,0,6,5,n,"",n,1,1,1,0,n,n,100);
it=s0.addItem(2,3,4,"Horizontal <br>&nbsp submenus &nbsp",n,n,"","",n,n,n,"","new");
var s1=it.addSubmenu(1,0,-4,2,0,0,0,6,10,n,"",n,1,1,0,1,n,n,100);
it=s1.addItem(11,12,13,"&nbsp&nbsp Item1",n,n,"","",n,n,n,n,n);
it=s1.addItemWithImages(11,12,13,"&nbsp Item2",n,n,"",n,n,n,0,0,0,2,3,2,"",n,n,n,n,n);
var s2=it.addSubmenu(1,0,-4,2,0,0,0,6,5,"&nbsp Title here &nbsp","",n,1,1,0,1,n,n,100);
it=s1.addItem(11,12,13,"&nbsp&nbsp Item3",n,n,"","",n,n,n,n,n);
it=s1.addItem(11,12,13,"&nbsp&nbsp Item4",n,n,"","",n,n,n,n,n);;

You cannot 'pass' an array from JSP to JavaScript, but I think what you need to do is quite simple:

In your JSP

<script type="text/javascript">
var javascriptArray = [
<% for (int i = 0; i < jspArray.length; i++) { %>
<%= i == 0 ? "" : ", " %> '<%= jspArray[i] %>'
<% } %>
nikhilbansalAuthor Commented:
Dear All,

I have made a mistake in my question. Instead of vector I have written varray. This might be quite confusing.

I feel sorry for it. Actually I have been working on PL SQL  and Java and so I made this mistake.


Nikhil Bansal
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

ZvonkoSystems architectCommented:
Vector does not help. It is absolutely of no use. The vector value is valid only in the database access context. At next call it does not have no meaning in new process and new storage.

How did you want to reuse that vector pointer? What do you want to achieve? Could you use some record counter?

<script type="text/javascript">
var javascriptArray = [
<% for (int i = 0; i < vector.size(); i++) { %>
<%= i == 0 ? "" : ", " %> '<%= vector.get(i) %>'
<% } %>
I would like to raise one question:
 - Why not use jsp to build an html-based menu, using one of many good css-powered menu's?
nikhilbansalAuthor Commented:
Hi Zvonko

Cud u plz visit the site and go there to the menu. There you can see below "Overview" a menu having "Menu Compatibility" etc. When i position my mouse over Menu Compatibility then it cascades to Supported Browsers,  Unsupported Browsers and Report Test Results.

Now for developing such a menu, they have hard - coded everything. I want it to be dynamic in nature.

My only question is tht can this dynamism be achieved for developing such a menu and if yes, then how ???

Plz visit the link provided as it will make the picture clearer.

ZvonkoSystems architectCommented:
I see that their demo/menu_items.js is highly structured and can be produced by your server side scripting as well.
And it need not to be placed outside the page in an extra file. You can embed your menu structure in a <script> section on your page.

nikhilbansalAuthor Commented:
Dear Zvonko

I visited the link They are having a horizontal sub - menu.

I tried developing tht but cudn't succeed.

Cud u plz help.

I want the code which gives a horizontal sub - menu

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.