Passing parameters from JSP to JavaScript

O

Omar

Hi!

I have a JavaScript function that receives an Array with words. This
Array can be gotten from a query to a DB using JSP.

The JavaScript function is executed when onFocus event occurs:
<input type='text' onFocus='myFunction(this.event, myArray);' />

myArray is declared as:
<script>
var myArray = new Array('j'habite', 'tu habites', 'il habite');
</script>

I want to tell JavaScript:
var myArray = myJSPArray;
Where myJSPArray is declared some where in the JSP document.

Is it possible? If so, how can I do it?
TIA!
 
T

Tom Dyess

Omar said:
Hi!

I have a JavaScript function that receives an Array with words. This
Array can be gotten from a query to a DB using JSP.

The JavaScript function is executed when onFocus event occurs:
<input type='text' onFocus='myFunction(this.event, myArray);' />

myArray is declared as:
<script>
var myArray = new Array('j'habite', 'tu habites', 'il habite');
</script>

I want to tell JavaScript:
var myArray = myJSPArray;
Where myJSPArray is declared some where in the JSP document.

Is it possible? If so, how can I do it?
TIA!

No, you'll need to create the array dynamically in the jsp code in js for
the other js to access it.
 
R

RobB

Omar said:
Hi!

I have a JavaScript function that receives an Array with words. This
Array can be gotten from a query to a DB using JSP.

The JavaScript function is executed when onFocus event occurs:
<input type='text' onFocus='myFunction(this.event, myArray);' />

myArray is declared as:
<script>
var myArray = new Array('j'habite', 'tu habites', 'il habite');
</script>

I want to tell JavaScript:
var myArray = myJSPArray;
Where myJSPArray is declared some where in the JSP document.

Is it possible? If so, how can I do it?
TIA!

You can tell JavaScript anything you want - but in the end, it has no
knowledge of what goes on at the server, it just processes whatever you
load into the (local) programming environment. This:

var myArray = myJSPArray;

....'tells' JS to set a reference at variable myArray pointing to
something at variable myJSPArray...if there's a JS array there, fine,
although the utility of the reference is unclear. As mentioned, there's
no point in downloading jsp code to the JavaScript engine, which
doesn't speak it.

This looks wrong:

onFocus='myFunction(this.event, myArray);

You pass a handable Event object inside an HTML event handler with the
local variable *event*. 'this' refers to the Input object in this
context.

If 'myArray' is global - not necessarily the best thing - you don't
need to pass it anyway, as it'll be visible to the function as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top