Omar wrote:
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.