Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I see script injected using GWT ScriptInjector?

Tags:

gwt

I'm trying out using GWT's ScriptInjector for the first time, and was wondering why I can't see the injected script as a tag anywhere in the page when I view page source. I'd expect it to show up in the page head.

I'm using a dead simple example, which works in that it displays the alert. I'm just wondering why I can't physically see it injected in the page. Also if I declare a JavaScript variable inside my script, it doesn't seem available on the global scope.

ScriptInjector.fromString("window.alert('testing');")
.setWindow(ScriptInjector.TOP_WINDOW)
.inject();
like image 485
praetorian1 Avatar asked Apr 07 '14 17:04

praetorian1


1 Answers

ScriptInjector works by injecting your js snippet into the top level window object and, by default, remove it just after it got evaluated (if you used fromString(); with fromUrl() the element is not removed by default). This is why you do not see it, but it actually executes.

If you want to keep the injected script element, just use setRemoveTag(false) on your builder FromString object, i.e.:

ScriptInjector.fromString("window.alert('testing');")
  .setRemoveTag(false)
  .setWindow(ScriptInjector.TOP_WINDOW)
  .inject();
like image 197
Andrea Boscolo Avatar answered Sep 29 '22 11:09

Andrea Boscolo