Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to transfer a Javascript valiable as parameter to a jsp method cal

Status
Not open for further replies.

royboy75

Programmer
Feb 13, 2007
114
0
0
GB
Hello,

I have this function in Javascript, which is calling a server-side Java method and is working fine:

Code:
function setCountryData(currentObj)
{
	var country = currentObj.title;
	currentObj.title = '<%=popC.getCountByCountry()%>'
}

I would like to tansfer the var country ar a parameter to getCountByCountry(), how do I do it?
 
You cannot. Server-side code is executed first, before the client-side code is output.

You'll have to either submit a form, or visit a URL with a param (whether by AJAX, iframe, form get, etc).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

Can you show me a simple example of such combination when submitting a form?
 
Sure:

Code:
<form method="post" action="yourServerSidePageURL.html">
   <input type="text" name="country" value="United Kingdom" />
   <input type="submit" />
</form>

As for the server-side part, you'd need to ask in the Java forum.

You could change the text field to be "hidden", and/or change the form's method to be "get", should you wish.

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

Now that I am thinking about it I don't want to submit the form. What I have is a client image map of the globe. Whenever the user hovers on a country the above javascript function is being activated. It calls a java object that, based on the country the user is hovering above, pulls the count data of this country from a DB. The problem is that this objects needs the country name from the relevant html <area> tag. Therefore I don't see how moving away from this page will solve this problem.

Roy
 
Here is my entire page:
Code:
<%@ page language="java" import="com.bp.gpd.services.POPCount" %>

<html>
<head>

<%!
String country = null;
POPCount popC = new POPCount();
%>

<script language="JavaScript">
function setCountryData(currentObj)
{
	currentObj.title = '<%=popC.getCountByCountry()%>'
}
</script>
</head>

<body>

<div>

<img src="../../images/earth.jpg" usemap="#earth" style="border-style:none" />

</div>

<div>
<map id="earth" name="earth">
<area shape="rect" coords="219,179,321,235" nohref title="US" onMouseOver="setCountryData(this)"/>
<area shape="default" nohref="nohref" alt="" />
</map>
</div>
</body>

</html>

It is working great but only for a hardcoded country since I cannot pass it as a parameter...

Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top