0

I am building an extention which first gets the window associated with an HTTPRequest as explained here.

There is a div element in the document which has a src from an external website. I basically cancel the request and get the associated window.
Now say I want to fill that window's doc with a string "Hello World".

Using the following in JavaScript (JSNI) works (ie, it replaces the string where normally the data from external source would be):

window.document.write("Hello World");

But I really need to do that in Java rather than through JSNI.

I tried using the class Document to pass the object making the call from JSNI as:

@[package].[class]::populateBox(Lcom/google/gwt/dom/client/Document)(window.document);

The method is defined as:

public static void populateBox(Document doc){
  doc.getBody().setInnerHTML("Hello World);
}

This code rather than replacing text at the div where the request was to be loaded replaces the top level body of the html document.

What is the problem here? Is Document the wrong class to use here?

Community
  • 1
  • 1
Saurabh Agarwal
  • 507
  • 2
  • 5
  • 16

1 Answers1

0

There is no problem with your Code:

window.document donates th the Document.

doc.getBody() will be the complete body of the Document.

doc.getBody().setInnerHTML(""); will reomove the complete content and set the body to a new value.

I think you are looking for appendChild:

DOM.appentChild(doc.getBody(), new HTML('Hello World!').getElement());
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • 2
    The HTML class is a widget, not an element, so can't be appended directly. The JSNI code could also be written as `$doc.innerHTML="Hello World"`. But adding a widget would be better expressed as something like `RootPanel.get().add(new HTML("Hello world"))`. – Colin Alworth Jun 04 '12 at 16:07