0
$('area').on('mouseover', function() {
    var territory = $(this).attr('name');
    var territoryString = new String(territory);
    var owner = <%game.getTerritory(territoryString).getOwner().getName(); %>;

    $('#selection').html(territory);
});
});

I'm trying to get the data from the Game object which I have stored as a JSP object. The territory I am trying to get is located within the game object as a list of territories. Is there a way to pass in the javascript variable as a parameter? Or do I need to make a JSP object with the list of territories?

shivajichandran
  • 143
  • 1
  • 2
  • 11
  • 1
    no, there is not like that because the jsp is processed at the server side and the javascript in the client side – Arun P Johny Jul 24 '13 at 15:38
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Jul 24 '13 at 15:39
  • The solution here is to use an ajax request which could sent the selected `territory` to server and server can return the value of the `owner` – Arun P Johny Jul 24 '13 at 15:39

1 Answers1

0

You can't directly. You have two options: Either you load all the territories names in your client stript the first time you load the page, and then you ask the territory for the specific territoryString using this client side object something like that:

var Territories = <% Print a json string map with the territories info with the server side code %>

$('area').on('mouseover', function() {
     .....
     var owner = Territories[territoryString].name;
      ....

the problem with this method is that your client side object wont be actualised if the server data change.

Other way is to use an ajax call in your click function, then loading the result in your JavaScript variable.

If you really want something in real time there are some comercial frameworks available to do automatic synchro between server and client variables, exemple: "pusher"

ElLocoCocoLoco
  • 387
  • 3
  • 11