Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to call javascript function defined in contents of HTMLPane

    Hi,

    I have an HTMLPane whose HTML text defines a javascript function, like the example below:

    Code:
    <HTML>
    <HEAD>
      <SCRIPT language='javascript'>
         function myfunction() {
           alert('Function called!');
         }
      </SCRIPT>
    </HEAD>
    </HTML>
    I want to call that JS function from my SmartGWT code, as an action for a button click, for example. I know JSNI is the way to go, but I couldn't get it working yet, so I'd like some help on how that can be done.

    What I did actually was to create a native method that calls the function as a window object, and call that method on the button click handler

    Code:
       // The HTMLPane argument is not used
       private native void callMyFunction(HTMLPane p) /*-{
         $wnd.alert("Will call myfunction");
         $wnd.myfunction();
       }-*/;
    
       (...)
       IButton b = new IButton("Call JS function");
       b.addClickHandler(new ClickHandler() {
         @Override
    	public void onClick(ClickEvent event) {
    	  callMyFunction(myHTMLPane);
    	}
       });   
       (...)
    But the code above just works on Firefox, on IE I got an error message saying that this property or method is not supported.

    So could any of you please give me some ideas on how to do that? Is the function defined in the HTMLPane visible as a window object, or is there any other way to call that?

    Thanks,
    Matheus

    #2
    I know in your message you say you still couldn't get JSNI to work.
    But I don't know how it could be broken at all.

    I used this documentation to achieve the exact result you are looking for:

    http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html

    http://courses.coreservlets.com/Course-Materials/pdf/ajax/GWT-JSNI.pdf

    note your method must be declared as "native"

    private native void alert1(String message) /*-{
    $wnd.alert(message);
    }-*/;

    Comment


      #3
      Hi, elviejo!

      Thanks for your response. I forgot to mention the fact that the contents of the HTMLPane, which declares the javascript function I want to call, is set as a result of an ajax request, after the page had been rendered.

      So I think the problem is that Internet Explorer does not interpret the <script> block of the content returned in ajax callback, although the HTML is rendered. Looking on some javascript sites I read that it may be necessary to call 'eval' on the javascript functions returned from ajax callbacks to get it working on IE. On firefox it seems this is not necessary (see http://stackoverflow.com/questions/510779/calling-javascript-function-returned-from-ajax-response)

      So, I think the solution for this case is to process the returned HTML code, extract the javascript function that defined the function and add a call to eval which receive as argument this javascript fragment. I didn't do that yet because it's not easy for me to extract the javascript from the HTML, but in some test where just the javascript function is returned from ajax callback the function became available for JSNI calls.

      [],
      Matheus

      Comment

      Working...
      X