Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing variables between scripts....

    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

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

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

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    url is a string so you need to embed it in between quotes.
    Code:
    x = "<%= url %>";
    alert(x);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The same way as you did in url variable.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahh...yes, but the variable is then out of scope and the value cannot be retrieved.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Then make it a global variable by declaring it inside <%! ... %> tags. This is JSP right?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •