HOWTO access page js variable?

0 views
Skip to first unread message

glongman

unread,
May 22, 2006, 3:57:35 PM5/22/06
to Google Web Toolkit
in the html I have declared a variable:

var pingTime = 10000;

and later <div id="widget"></div> - where my widget is inserted.

I tried the following:

public native int getPingTime() /*-{
return pingTime;
}-*/;

but it failes with expection:

[ERROR] Unable to load module entry point class
com.alcea.mitesite.chat.client.MiteSiteChatDemo
java.lang.RuntimeException: JavaScript method
'@com.alcea.mitesite.chat.client.MiteSiteChat::getPingTime()' threw an
exception
<snip!>
Caused by: com.google.gwt.core.client.JavaScriptException: JavaScript
TypeError exception: 'pingTime' is undefined
<snip!>

any ideas?

(unnabridged exception trace below)

Geoff

[ERROR] Unable to load module entry point class
com.alcea.mitesite.chat.client.MiteSiteChatDemo
java.lang.RuntimeException: JavaScript method
'@com.alcea.mitesite.chat.client.MiteSiteChat::getPingTime()' threw an
exception
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNative(ModuleSpaceIE6.java:365)
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNativeInt(ModuleSpaceIE6.java:176)
at
com.google.gwt.dev.shell.JavaScriptHost.invokeNativeInt(JavaScriptHost.java:87)
at
com.alcea.mitesite.chat.client.MiteSiteChat.getPingTime(MiteSiteChat.java:143)
at
com.alcea.mitesite.chat.client.MiteSiteChat.start(MiteSiteChat.java:78)
at
com.alcea.mitesite.chat.client.MiteSiteChatDemo.onModuleLoad(MiteSiteChatDemo.java:31)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:64)
Caused by: com.google.gwt.core.client.JavaScriptException: JavaScript
TypeError exception: 'pingTime' is undefined
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at
com.google.gwt.dev.shell.ModuleSpace.createJavaScriptException(ModuleSpace.java:267)
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.exceptionCaught(ModuleSpaceIE6.java:76)
at
com.google.gwt.dev.shell.JavaScriptHost.exceptionCaught(JavaScriptHost.java:31)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.google.gwt.dev.shell.StaticJavaDispatch.callMethod(StaticJavaDispatch.java:45)

Mat Gessel

unread,
May 22, 2006, 5:09:46 PM5/22/06
to Google Web Toolkit
Hi Geoff,

I don't know the anwser, but I can give you a tip. Compile with -PRETTY
and open the main page from /www in Firefox and use the Venkman
debugger to see what is what. If you open Venkman before loading the
page it will break on the exception.

-= Mat

Scott Blum

unread,
May 22, 2006, 6:05:37 PM5/22/06
to Google Web Toolkit
Try changing the declaration in your HTML to "window.pingTime = 10000",
and change your JSNI method to use "$wnd.pingTime".

glongman

unread,
May 23, 2006, 8:53:18 AM5/23/06
to Google Web Toolkit
ok. I changed the declaration in the html to:

window.pingTime = 10000;

and the method to

private native int getPingTime() /*-{
return $wnd.pingTime;
}-*/;

and I get a new exception:

[ERROR] Unable to load module entry point class
com.alcea.mitesite.chat.client.MiteSiteChatDemo
java.lang.RuntimeException: JavaScript method

'@com.alcea.mitesite.chat.client.MiteSiteChat::getPingTime()' returned
'undefined'. This can happen either because of a missing return
statement, or explicitly returning a value of 'undefined' (e.g. 'return
element[nonexistent property]')
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNative(ModuleSpaceIE6.java:342)

glongman

unread,
May 23, 2006, 9:34:51 AM5/23/06
to Google Web Toolkit
Ha! I found the problem. I have a js method that wants to call into my
widget and it doesn't parse which prevents my pingTime variable from
being created.

Which raises another question for another topic.

Thanks for the help!

Geoff

Reply all
Reply to author
Forward
0 new messages