Access JS variable from GWT

1,079 views
Skip to first unread message

Javid

unread,
Aug 16, 2006, 2:35:05 PM8/16/06
to Google Web Toolkit
Hi,
I recently upgraded to GWT 1.1.0 and I am having trouble accessing a
Javascript variable. In my index.html I have a js variable ip_address,
and in the GWT code I have a native method to access the variable. like
$wnd.ip_address, for some reason in the new version I get an undefined
value instead of a string.
Can someone tell me what could possibly be wrong??

Thanks,
Javid

Scott Blum

unread,
Aug 16, 2006, 4:23:27 PM8/16/06
to Google Web Toolkit
Was it you who reported the same thing on another thread? I haven't
followed up yet because I haven't had a chance to look into it, but my
immediate reaction was to wonder if your method was declared to return
the correct type. It looked like you were returning a String when you
had declared that you were returning a JavaScriptObject, but I may be
completely misinterpreting your other thread. I will follow up to the
other thread when I've had a chance to look.

Scott

Javid

unread,
Aug 16, 2006, 5:02:21 PM8/16/06
to Google Web Toolkit
Scott,
Yes I am the same person. Sorry for the cross post.
Here is some sample code to test the issue:
HTML file:
<head>
<script language="Javascript">
var ip_address = "10.0.0.1"
</script>

Java Native code:
private native JavaScriptObject getIP() /-* {
return $wnd.ip_address
} -*/;

System.out.println(getIP());

I tried both JavaScriptObject and String return type but I get
undefined exception. It would be very helpful if you could suggest me
some quick fix as my entire application depends on this, else I have to
downgrade to gwt-1.0.21

Thanks,
Javid

Scott Blum

unread,
Aug 16, 2006, 5:49:47 PM8/16/06
to Google Web Toolkit
This works for me in 1.1.0:

Hello.html:
<html>


<head>
<script language="Javascript">
var ip_address = "10.0.0.1"
</script>

<meta name='gwt:module' content='com.google.gwt.sample.hello.Hello'>
<title>Hello</title>
</head>
<body bgcolor="white">
<script language="javascript" src="gwt.js"></script>
</body>
</html>

Hello.java:
package com.google.gwt.sample.hello.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;

public class Hello implements EntryPoint {

public void onModuleLoad() {
Window.alert(getIP().toString());
}

private native String getIP() /*-{
return $wnd.ip_address
}-*/;

}


Trying to return JavaScriptObject does not work, but returning a String
does work for me. Any chance you're serving up a different html file
than the one you think? I might try putting an alert() into the block
that sets up the ip_address variable.

HTH,
Scott

Javid

unread,
Aug 16, 2006, 5:58:25 PM8/16/06
to Google Web Toolkit
Thanks Scott,
I tried with return type as String and it seems to work. I was using it
in the JSP file and I think there was some JSP part that prevented the
variable from creation.
thanks for your help again,

Javid

Reply all
Reply to author
Forward
0 new messages