Errors passing javascript variables to GWT

42 views
Skip to first unread message

dhoffer

unread,
Dec 14, 2012, 11:27:49 PM12/14/12
to google-we...@googlegroups.com
I'm following the example given here https://developers.google.com/web-toolkit/articles/dynamic_host_page of passing a global info javascript variable back to the GWT app when it loads.  E.g.
// In GwtHostingServlet's doGet() method...
writer
.append("<html><head>");
writer
.append("<script type=\"text/javascript\" src=\"sample/sample.nocache.js\"></script>");

// Open a second <script> tag where we will define some extra data
writer
.append("<script type=\"text/javascript\">");

// Define a global JSON object called "info" which can contain some simple key/value pairs
writer
.append("var info = { ");

// Include the user's email with the key "email"
writer
.append("\"email\" : \"" + userService.getCurrentUser().getEmail() + "\"");

// End the JSON object definition
writer
.append(" };");

// End the <script> tag
writer
.append("</script>");
writer
.append("</head><body>Hello, world!</body></html>");
Then in my GWT app's EntryPoint class I have:

public static native String getEmail() /*-{
  return $wnd.info['email'];
}-*/
;
However when the app runs I get an exception saying:

$wnd.info is undefined

I've tried $doc but got the same error.  I also tried window and document but then it says document.info is undefined.  What am I doing wrong?

-Dave

Thomas Broyer

unread,
Dec 15, 2012, 4:54:54 AM12/15/12
to google-we...@googlegroups.com
Have you tried swapping the order of the <script>s? Also, are you sure you don't have a parse error? (can you read the 'info' global var in your browser's dev tools? particularly, double-check that getCurrentUser().getEmail() doesn't contain a newline; you might want to use a JSON API to generate your JS object literal, such as AutoBeans, javax.json, Jackson, org.json, etc.)

David Hoffer

unread,
Dec 15, 2012, 10:48:55 PM12/15/12
to Google Web Toolkit
Yup your right, I had made what seemed like minor changes to the JS
literal code that was causing this problem. Odd as the errors didn't
seem to indicate a JS source error but my bad...it's working now.
Thanks.

-Dave
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/3DG_kMwl8eAJ.
>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-tool...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
Reply all
Reply to author
Forward
0 new messages