...

View Full Version : Passing variables between scripts....



QuickFix
05-12-2007, 12:26 AM
As a Javascript newbie, I am having trouble passing the contents of a variable from one script to another. I am trying to retrieve the contents of x and y that I have populated at the top of script:

<%


Object mainMapObj = session.getAttribute("map");
if (mainMapObj == null) {
RequestDispatcher rd = request.getRequestDispatcher("imfTimeout.jsp");
rd.forward(request, response);
return;
}

AimsMap map = (AimsMap) mainMapObj;
String ptsArg = request.getParameter("pts");
AimsPoints pts = new AimsPoints(ptsArg);

//response.setHeader("Expires","0");
//Response.setHeader("Pragma", "no-cache");
//response.setHeader("Cache-Control","no-cache");

AimsPoint pt = pts.getPoint(0);

String x = Double.toString(pt.getX());
String y = Double.toString(pt.getY());

out.print("UTMX " + x);
out.print("UTMY " + y);

%>

<html>
<head>
<title>Test</title>
</head>
<body bgcolor="#ffffff" onload="startup()">

<script>
alert(x); //Here is where I am trying to get the value of x and y to come up!
</script>

<br>
</body>
</html>

Any reply would be greatly appreciated.

Thanks in advance,

QF

cyberlogi
05-12-2007, 01:46 AM
In your example you have set x & y as Java variables, then you are trying to reference them as JavaScript variables. You first need to load your server side Java variables into JavaScript. Do this:


<script>
var x = <%= x %>;
var y = <%= y %>;
alert(x); // and X will now have a value
</script>

QuickFix
05-14-2007, 11:13 PM
Thanks a lot. That worked great! So great, I thought I would try it again...This time, it doesn't appear to work. Any ideas? I am trying to pass the value stored in the URL string to the Javascript part of the code.

<%


Object mainMapObj = session.getAttribute("map");
if (mainMapObj == null) {
RequestDispatcher rd = request.getRequestDispatcher("imfTimeout.jsp");
rd.forward(request, response);
return;
}

AimsMap map = (AimsMap) mainMapObj;

map.setWidth(250);
map.setHeight(250);
map.refresh();
String url = map.getUrl();
//response.sendRedirect(url);
out.print(url);

%>

<html>
<head>
<title>A</title>
</head>
<script>
function startup()
{
x = <%= url %>;
alert(x);
}

</script>

<body onload="startup()">

</body>
</html>

glenngv
05-15-2007, 02:14 AM
url is a string so you need to embed it in between quotes.

x = "<%= url %>";
alert(x);

QuickFix
05-22-2007, 04:39 PM
I have yet another question. This time I have a variable declared (PID) in a WHILE block and I need to get it out to my javascript:


while (! rs.EOF()) {
if (highlight) {
out.println("<tr bgcolor=\"#ffffcc\">");
} else {
out.println("<tr>");
}
//for (int i =11; i < rs.getFields().size(); i++) {
{ f = (AimsField) rs.getFields().elementAt(12);
if ((f.getType() > 0) && (f.canIdentify())) {
String fldValue = rs.valueAsString(f.getName());
if ((fldValue != null) && (fldValue.length() > 0)) {
if (fldValue.startsWith("http:") || fldValue.startsWith("https:") || fldValue.startsWith("ftp:")) {
fldValue = "<a href=\"JavaScript:OpenLink('" + fldValue + "')\">" + f.getHypertextMessage() + "</a>";
}
String PID;
PID = f.format(fldValue);
out.print(PID);
out.println("<td class=\"bn8\" nowrap>" + f.format(fldValue) + "</td>");

} else {
out.print("<td align=\"left\" class=\"bb8\">&nbsp;</td>");
}
}
}
out.println("</tr>");
recordCount++;
highlight = ! highlight;
rs.moveNext();

How do I pass the contents of the PID variable out?

Thanks in advance,

M

glenngv
05-22-2007, 06:03 PM
The same way as you did in url variable.

QuickFix
05-22-2007, 06:07 PM
ahh...yes, but the variable is then out of scope and the value cannot be retrieved.

glenngv
05-22-2007, 06:14 PM
Then make it a global variable by declaring it inside <%! ... %> tags. This is JSP right?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum