Reputation: 183
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();
Upvotes: 5
Views: 3074
Reputation: 3048
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();
Upvotes: 7
Reputation: 46841
Please have a look at below sample.
I have modified the inner HTML of a div
. The changes are visible in Firebug but if you view the source it will be not there.
EntryPointClass:
ScriptInjector.fromString("document.getElementById('mydiv').innerHTML='hi';")
.setWindow(ScriptInjector.TOP_WINDOW).inject();
HTML:
<html>
...
<body>
<div id='mydiv'></div>
</body>
...
</html>
Snapshot:
View Source:
Upvotes: 0