0

As I get some problem with integrating Html code from designer work in Gxt, I decided to run very simple test and start by using the Gwt HtmlPanel, and I can't explain why the javascript included is not rendered.

public class TestHTML implements EntryPoint {

    public void onModuleLoad() {

        String contenu = "<html>"
         +"<head>"
        +" <script type='text/javascript'>"
        +"function functionOne() { alert('You clicked the top text'); }"
        +"function functionTwo() { alert('You clicked the bottom text'); }"
        +"</script>"
        +"</head>"
        +"<body>"
         +"<p><a href='#' onClick='functionOne();'>Top Text</a></p>"
         +"<p><a href='javascript:functionTwo();'>Bottom Text</a></p>"
         +"</body>"
        +"</html>";
          HTMLPanel htmlPanel = new HTMLPanel(contenu);
          ContentPanel content = new ContentPanel();
          content.add(htmlPanel);
          RootPanel.get().add(content);

    }
} 

I can't find out the javascript function in the browser (I looked with firebug) Is there somebody with an answer or advise ? Regards

Alain BUFERNE
  • 2,016
  • 3
  • 26
  • 37

1 Answers1

2

You should make use of JSNI in gwt.

public void onModuleLoad() {
    registerJS();
    String contenu = "<html>"
            +"<body>"
             +"<p><a href='#' onClick='functionOne();'>Top Text</a></p>"
             +"<p><a href='javascript:functionTwo();'>Bottom Text</a></p>"
             +"</body>"
            +"</html>";
              HTMLPanel htmlPanel = new HTMLPanel(contenu);
              RootPanel.get().add(htmlPanel);
}

public native void registerJS()/*-{
    $wnd.functionOne = function(){
        alert('you clicked the top text');
    }

    $wnd.functionTwo = function(){
        alert('you clicked the bottom text');
    }
}-*/;
David
  • 4,185
  • 2
  • 27
  • 46
  • Thank you very much. As my problem originally was more complicated I have to dig starting from your information. Is there a way of being in touch by mail? – Alain BUFERNE Nov 09 '12 at 15:55