On Wed, Dec 3, 2008 at 18:08, David H. Cook <[EMAIL PROTECTED]>wrote:
>
> I'm coding my first GWT app, and trying to get my head around
> creating an anchor that calls native javascript. It now occurs to me
> that even tho the goal is to write in java, there will still be some
> things that can only be done cleanly by utilizing some native
> javascript. (Agreed?)
For native JavaScript invocations, try to use Java method declarations, but
mark the method as "native" and use GWT's JSNI feature to inline the
JavaScript code. It makes it far easier to pass values into JavaScript from
Java, and to return values from JavaScript back into Java. If you need to
hold onto a plain JavaScript object or function, use the JavaScriptObject
type in Java. There's plenty more details in the GWT docs under "JavaScript
Integration" or some such section.
Given that, can someone point me to some sample GWT code
> using such anchors?
If you really really want to use a javascript: sort of URL, I think you can
use an Anchor widget:
add(new Anchor("click here", false, "javascript:mycode();"));
This generates the same as:
<a href="javascript:mycode();">click here</a>
Another option is to use an Anchor with a ClickListener and JSNI:
Anchor a = new Anchor("click here", false);
a.addClickListener(new ClickListener() {
public void onClick(Widget w) {
callMyCode();
}
});
add(a);
private static native void callMyCode()/*-{ mycode(); }-*/;
And, can someone explain when to use
> the anchor constructor with the 2nd boolean arg 'asHTML' set
> true and when to set it false?
Marking it true (asHTML) means the text passed is set as the innerHTML,
rather than as the innerText of the anchor element. E.g.:
add(new Anchor("<blink>click here", false, "javascript:mycode();"));
yields:
<a href="javascript:mycode();"><blink>click here</a>
but this produces the dreaded "blink the entire page" effect:
add(new Anchor("<blink>click here", true, "javascript:mycode();"));
yields:
<a href="javascript:mycode();"><blink>click here</a>
asHTML = true is generally a bad idea, unless you know exactly where that
input came from. It can be useful if your code is producing the HTML, or
you read it from a known resource, but generally I have found you want
asHTML = false because you want to avoid HTML injection attacks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---