FAQ

How to execute arbitrary Javascript on the Client Side

hi dominique and benjamin,

even though our script manager is still quite rudimentary, it does in fact allow you to execute arbitrary javascript on the client side. in org.wings.plaf.css.script there are 2 simple classes dealing with this:

- OnPageRenderedScript
- OnHeadersLoadedScript

both of them implement the ScriptListener interface and can therefore be added to the script manager. scripts encapsulated in the first class get executed as soon as the page/dom is rendered/updated completely. scripts encapsulated in the second class get executed as soon as all js-headers have been loaded completely. so in your case something like this should work:


final StringBuffer script = new StringBuffer();
script.append("var x = window.open('http://www.google.de','my_window',")
       .append("'width=800,height=600,toolbar=0,menubar=0,location=0,")
       .append("status=1,resizable=1,scrollbars=1'); x.focus();");

final SButton open = new SButton("Open window!");
open.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         ScriptManager.getInstance().addScriptListener(
             new OnPageRenderedScript(script.toString()));
     }
});
SForm formPanel = new SForm();
formPanel.add(open);


furthermore, such scripts are transient since they are evaluated within the window context and not bound to any dom element (unless your script does this binding manually). hence you don't have to "remove" anything here, just use the above code. whenever the button is clicked the script is sent to the client and evaluated there.

regards,
stephan