I struggled for a long time trying to get an Adsense ad to appear in a <div>, but that seems to be the wrong approach. Divs are nice for styling reasons, but it seems that Adsense knows when it's in a div, and won't display.
I avoided IFrames (which is what the GWT Frame object compiles to) because sizing isn't automatic. Eventually, though, it became apparent that IFrames were the way to go, since Adsense ads will load in them. I create an IFrame and point it at a static page that contains the necessary Adsense script. That just works. The content loads correctly, and ads will display.
But here's the rub. IFrames need to be sized with custom javascript. I use this javascript: https://penwag.com/home/iframe.js. This works easily for all browsers except - you guessed it - IE. Below is the GWT code that I use to bring it all together, including a work-around for IE.
public static native String getUserAgent() /*-{
return navigator.userAgent.toLowerCase();
}-*/;
private Widget buildMainPanel() {
Widget mainPanel;
if(getUserAgent().contains("msie")) {
mainPanel = buildIEPanel();
} else {
mainPanel = buildNonIEPanel();
}
mainPanel.getElement().setId(getPanelId());
mainPanel.addStyleName(Styles.StaticPanel);
return mainPanel;
}
private Widget buildNonIEPanel() {
Frame mainPanel = new Frame();
mainPanel.getElement().setAttribute("onLoad", "resizeCaller();");
mainPanel.setUrl(getRootPage());
return mainPanel;
}
private Panel buildIEPanel() {
Panel mainPanel = new VerticalPanel();
HTML adBar = new HTML("Loading...");
mainPanel.add(adBar);
try {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + "ie_ads/index.html");
builder.sendRequest(null, new RequestHandler(adBar, null));
} catch (RequestException e) {
adBar.setHTML("");
}
HTML content = new HTML("Loading...");
mainPanel.add(content);
String errorMessage = "Failed to load content, please try again later.";
try {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + getRootPage());
builder.sendRequest(null, new RequestHandler(content, errorMessage));
} catch (RequestException e) {
content.setHTML(errorMessage);
}
return mainPanel;
}
3 comments:
what exactly means getRootPage()??
Oh, sorry. It was:
protected String getRootPage() {
return "resources/index.html";
}
So, essentially, it just returns the main landing page of the site.
Thanks Don!
Anyway I can't make it work. I made some modification related to GWT2.0
public void onModuleLoad() {
RootPanel.get().add(buildMainPanel());
}
private Widget buildMainPanel() {
Widget mainPanel;
mainPanel = buildNonIEPanel();
return mainPanel;
}
private Widget buildNonIEPanel() {
Panel mainPanel = new VerticalPanel();
final HTML adBar = new HTML("Loading ...");
final HTML content = new HTML("Loading...");
mainPanel.add(adBar);
try {
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,"/testframe.html");
builder.sendRequest(null, new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
adBar.setHTML("ERROR");
}
@Override
public void onResponseReceived(Request request,
Response response) {
adBar.setHTML(response.getText());
}
});
} catch (RequestException e) {
adBar.setHTML("Catch");
}
mainPanel.add(content);
try {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,"/Test.html");
builder.sendRequest(null, new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
adBar.setHTML("ERROR");
}
@Override
public void onResponseReceived(Request request,
Response response) {
content.setHTML(response.getText());
}
});
} catch (RequestException e) {
adBar.setHTML("can't");
}
return mainPanel;
}
Post a Comment