GWT - How to change title of the window browser

Let's say that you need to change programmatically the title of your WEB browser window.

For this you have to play with the GWT Document class, like in the below code, where we define a static function to help us:

import com.google.gwt.dom.client.Document;
...
public static void setBrowserWindowTitle (String newTitle) {
    if (Document.get() != null) {
        Document.get().setTitle (newTitle);
    }
}

It's not rocket science, but it's very cool to know simple tricks, which help you to do what you want.  Also, if you use a widgets framework like GXT (Ext GWT), SmartGWT, or GWT-Ext, be careful not to use their Document class, but the one from the GWT package, like it's shown above. Those new 'Document' classes not always behave the same :-) like the GWT.

Cheers!

6 comments:

  1. Was struggling with setting the title using a native js method in smartgwt and this solved it, thanks!

    ReplyDelete
  2. I am very glad it helped -- Cheers!

    ReplyDelete
  3. When I get the html page using HtmlUnit-2.8 I cannot see the Title setted by Document.get().setTitle (newTitle);

    Any explanation?

    Cheers

    Mirko

    ReplyDelete
  4. Hi Mirko,

    the HtmlUnit is not supported officially by the GWT, so I cannot advice of what GWT does when si ran on this browser. How is it working on IE, FF, Chrome, Safari, Opera ?

    Cheers!

    ReplyDelete
  5. Works fine on IE, FF, Chrome, Safari, Opera browser.

    I use HtmlUnit just for crawler.

    Many thanks

    Mirko

    ReplyDelete