Monday, July 13, 2015

Re: How to avoid the anchor from opening and navigating to another web page?

How about this:

NodeList<Element> anchors = myWidget.getElement().getElementsByTagName("a");
for(int i = 0; i < anchors.getLength(); i++) {
Element anchor = anchors.getItem(i);
anchor.setAttribute("target", "_blank" );
}

Am Montag, 13. Juli 2015 08:04:20 UTC+2 schrieb Victor Zhang:
Hi all,

I have a panel which includes the widget HTML, as you know it's kind of SPAN or DIV something, and each time when I get the html content from server side I will populate the widget like:
myWidget.setHTML(htmlContent); It works well but one thing that is if the html content has anchor inside and each time when user clicks it a new url will be visit and the whole gwt application will be gone, that means it's out of control then. Then how to stop this happening is a headache of mine.
I tried this code to add a handler to the click event each time when the HTML widget is clicked and if the target is an anchor then add a '_blank' target to it.
//                //com.google.gwt.dom.client.Element e = Element.as(event.getNativeEvent().getEventTarget());
//
//                    if(e.getNodeName().equalsIgnoreCase("A")) {
//                        String targetUrl=e.getAttribute("target").trim();
//                        if(targetUrl==null || targetUrl.equals(""))
//                        {
//                            e.setAttribute("target", "_blank");
//                        }
//                    }
This used to work but recently I found it becomes invalid, each time the handler is triggered the event target is always the "SPAN" no matter what objects I clicked.Is there anything wrong? I was using GTW 2.5 and now 2.6. Is it caused by the GWT? or the version of the browser? Appreciate if someone can shed a light on me, thanks.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment