Hi there, good folks.
Today we had a long discussion on the public Vaadin skype-chat about if Vaadin should allow running of native JavaScript as such (easily). There has been a lot of hacks for it, for example put the javascript into a label and have the browser interpret it as XHTML. Nothing worked easily and perfect and this was what the discussion was about: should Vaadin have built in support?
I, personally, was against it, for a couple of reasons. For example, it smells like a big security risk where you really can mess up something, it is a bloat-feature most won’t ever use and Vaadin tries to hide all the low-level code from the coder. That’s why it is so easy to use Vaadin. Also, I hate Javascript, a lot!
One suggestion that popped up was that it could be a custom widget, that you could add to your project if you really really need it. That way it won’t bloat Vaadin, and it’s at that point “use at own risk”. The implementation is also quite simple and it would be a nice addition for the upcoming Directory.
Therefore, I made a component that can do just this, run any javascript you want. The API contains only three functions (+ vaadin own componentfunctions) and these are setJavascript(string), getJavascript() and execute(). The usage looks like this:
NativeJS nativejs = new NativeJS("alert('Hi!')");
nativejs.execute()
where you can bind the execute function behind some button, or run on startup, and you can change the javascript in question with setJavascript(string);
Here’s a screenshot of my demo application:
You can test
the live demo is found here
. Try, for example, writing into the text field window.print();
No sources just yet, because I’m home right now, and my svn username/password is at work, so I don’t have access for the moment. I’ll try to put it there tomorrow, so that it can be found on dev.vaadin.com svn-repo under incubator/NativeJS.
If you have any suggestions on how to enhance this, send me a message.