PDA

View Full Version : Displaying <script> elements



weazelb0y
2 Jun 2010, 12:33 PM
I have a bunch of scripts in a database.

1. <script>document.write('hi');</script>
2. <script src="http://somewhere.com/something.js"></script>
etc...

I would like to display them in a row one after another. Just creating a Html object and doing a setHtml() with the code does not work.

Is there a way to do what I'm looking for?

Thanks!

Colin Alworth
3 Jun 2010, 10:42 AM
Your first example will erase the entire page if run after page load, and all GWT apps are loaded after page load, because of how document.write works. Your second example should work as is (at least in some browsers) - just append the Html object to the page.

If I have misread, and you are intending to just draw these tags and content so the user can read them and not execute them, then you should look into GWT's Document.get().createTextNode(String) or Element.setInnerText(String).

weazelb0y
4 Jun 2010, 9:14 AM
Sorry if I was unclear, but I do want to execute the code in the script. Here is an example that does not work...

String fromDatabase = "<script>document.write('script I want to execute');</script>";
final Html html = new Html(fromDatabase);
Viewport v = new Viewport();
v.setLayout(new FillLayout());
v.add(html);
RootPanel.get().add(v);

Colin Alworth
4 Jun 2010, 11:16 AM
This will be fun.

Your document.write will never work well, unless you are going to iframe every one of these, as any call to document.write after the page is loaded means erasing the entire currently loaded page.

Loading remote javascript should already work, as I mentioned. Running javascript within two tags may work in one browser, but not another - are you observing that this never works?

Worst case, you need to create an iframe, and use it's internal document object to write() out the script tag to it. This will also be annoying in terms of whether you want to write all the fragments to the same page, or whether they should run all on the same page.

What is the point of this feature? The ability to run these lines all at once, or one at a time? Is there output that should be rendered so the user can see them, or is this automated to simulate some other functionality? And is it not possible for the server to just write a simple page with all of these script tags on it when the user loads the page, instead of using GWT for it?