How to load an external JS-file without calling a function in there

173 views
Skip to first unread message

newnoise

unread,
Feb 29, 2012, 8:14:35 AM2/29/12
to Google Web Toolkit
Hi,

I'm trying to dynamically add advertisement to our page. Our
advertisement-partner provides us a script which we have to include
like this:
<script type="text/javascript" src="http://www.somesource.com/
output.js?id=123"></script>

The output.js just contains one line of code like this:
document.write("<div>some html</div>");

I have no idea how I can add this JS to the page and make it
execute ... I tried just adding it as a HTML-Widget, which correctly
adds the code, but it isnt executed. Also the ScriptInjection doesnt
help, because I need the code somewhere in the <body> and not in
<head>.

I'm happy for any help!
Tom

Thomas Broyer

unread,
Feb 29, 2012, 9:55:13 AM2/29/12
to google-we...@googlegroups.com
Anything relying on document.write() must be in your HTML source when it's being parsed; they can't be loaded dynamically.

salk31

unread,
Feb 29, 2012, 10:11:00 AM2/29/12
to google-we...@googlegroups.com
If you know the size of ad ahead of time then iframe?

JoseM

unread,
Feb 29, 2012, 11:55:07 AM2/29/12
to google-we...@googlegroups.com
Check their API or read their javascript code, there might be a way to pass an id of a div which they would use to append to instead of using document.write.  That's what I found when trying to use Twitter's Widget api, it was documented but I did find it by reading their code and seeing what it does.

Joseph Lust

unread,
Feb 29, 2012, 9:14:21 PM2/29/12
to Google Web Toolkit
newnoise,

I thought you could just place the <script> tag in your manifest file
and that external library would be loaded into the page. Perhaps
you've already tried this, but I did not see it mentioned above.

i.e.

<module rename-to='app'>
...
<script src="advert.js"></script>
...

Otherwise, can you just look at their API as JoseM pointed out and
write a few lines of JNSI to wrap it and then have your own java
addAdvert(Element div) method? That would be the cleanest solution.
You should be able to use a JS Beautifier (many browser based versions
available) to deobfuscate it if it's not document and find the
functions you need to call.

Since it might not be a supported API, also add some GwtTestCases to
confirm proper functionality of the JNSI hook so you know if an
upgrade from the advertiser breaks it.


Sincerely,
Joe

On Feb 29, 11:55 am, JoseM <jose.a.marti...@gmail.com> wrote:
> Check their API or read their javascript code, there might be a way to pass
> an id of a div which they would use to append to instead of using
> document.write.  That's what I found when trying to use Twitter's Widget
> api, it was documented but I did find it by reading their code and seeing
> what it does.
>
>
>
>
>
>
>
> On Wednesday, February 29, 2012 9:55:13 AM UTC-5, Thomas Broyer wrote:
>
> > Anything relying on document.write() must be in your HTML source when it's
> > being parsed; they can't be loaded dynamically.
>
> > On Wednesday, February 29, 2012 2:14:35 PM UTC+1, newnoise wrote:
>
> >> Hi,
>
> >> I'm trying to dynamically add advertisement to our page. Our
> >> advertisement-partner provides us a script which we have to include
> >> like this:
> >> <script type="text/javascript" src="http://www.somesource.com/
> >> output.js?id=123 <http://www.somesource.com/output.js?id=123>"></script>
>
> >> The output.js just contains one line of code like this:
> >> document.write("<div>some html</div>");
>
> >> I have no idea how I can add this JS to the page and make it
> >> execute ... I tried just adding it as a HTML-Widget, which correctly
> >> adds the code, but it isnt executed. Also the ScriptInjection doesnt
> >> help, because I need the code somewhere in the <body> and not in
> >> <head>.
>
> >> I'm happy for any help!
> >> Tom
>
> On Wednesday, February 29, 2012 9:55:13 AM UTC-5, Thomas Broyer wrote:
>
> > Anything relying on document.write() must be in your HTML source when it's
> > being parsed; they can't be loaded dynamically.
>
> > On Wednesday, February 29, 2012 2:14:35 PM UTC+1, newnoise wrote:
>
> >> Hi,
>
> >> I'm trying to dynamically add advertisement to our page. Our
> >> advertisement-partner provides us a script which we have to include
> >> like this:
> >> <script type="text/javascript" src="http://www.somesource.com/
> >> output.js?id=123 <http://www.somesource.com/output.js?id=123>"></script>
>
> >> The output.js just contains one line of code like this:
> >> document.write("<div>some html</div>");
>
> >> I have no idea how I can add this JS to the page and make it
> >> execute ... I tried just adding it as a HTML-Widget, which correctly
> >> adds the code, but it isnt executed. Also the ScriptInjection doesnt
> >> help, because I need the code somewhere in the <body> and not in
> >> <head>.
>
> >> I'm happy for any help!
> >> Tom
>
> On Wednesday, February 29, 2012 9:55:13 AM UTC-5, Thomas Broyer wrote:
>
> > Anything relying on document.write() must be in your HTML source when it's
> > being parsed; they can't be loaded dynamically.
>
> > On Wednesday, February 29, 2012 2:14:35 PM UTC+1, newnoise wrote:
>
> >> Hi,
>
> >> I'm trying to dynamically add advertisement to our page. Our
> >> advertisement-partner provides us a script which we have to include
> >> like this:
> >> <script type="text/javascript" src="http://www.somesource.com/
> >> output.js?id=123 <http://www.somesource.com/output.js?id=123>"></script>

newnoise

unread,
Mar 1, 2012, 11:06:19 AM3/1/12
to Google Web Toolkit
Hi Guys,

thanks for the input!
Finally I figured it out using the phenomenal writeCapture-library
(see: https://github.com/iamnoah/writeCapture/wiki). I linked it from
my module.gwt.xml-file and wrote a simple native method:

public static native void domWrite(String div, String src) /*-{
$wnd.writeCapture.html(div, src, function() {
});
}-*/;

And it works perfectly well!

Regards, Tom
Reply all
Reply to author
Forward
0 new messages