PDA

View Full Version : Contradictory behavior - same code... what gives?



jpnet
26 Mar 2009, 11:34 AM
I'm trying to get an onload event to fire on an Iframe. The code works as expected in HTML.



<html>
<head><title>test</title></head>
<body>
<script type="text/javascript">
ifrm = document.createElement("iframe");
ifrm.setAttribute("src", "http://myserver.com/mysuperlargefile.pdf");
ifrm.style.width.value = "100px";
ifrm.style.height.value = "100px";
ifrm.onload = function(){ alert('hi'); };
document.body.appendChild(ifrm);
</script>
</body>
</html>


You can copy and paste that into an html file and run it on your desktop. The Javascript alert is fired when the PDF is done loading. Now when I paste this same code into in a JSNI method the Javascript alert is not fired, however the PDF does load. Here is the JSNI code:


public native void createIFrame(String url) /*-{
ifrm = document.createElement("iframe");
ifrm.setAttribute("src", "http://myserver.com/mysuperlargefile.pdf");
ifrm.style.width.value = "100px";
ifrm.style.height.value = "100px";
ifrm.onload = function(){ alert('hi'); };
document.body.appendChild(ifrm);
}-*/;


Any thoughts on why the Javascript alert is not being fired when using the JSNI method?? Why does it work in a regular HTML page but not in JSNI?

This one has me stumped.

Thanks for your help.

-JP

sven
26 Mar 2009, 11:36 AM
document is $doc in jsni. Try that.

jpnet
26 Mar 2009, 11:43 AM
document is $doc in jsni. Try that.


Unfortunately it didn't work. As I said, the PDF still loads as expected. It's just that the Alert is not being fired. Any other thoughts?

The code has now been altered to:


public native void createIFrame(String url) /*-{
ifrm = $doc.createElement("iframe");
ifrm.setAttribute("src", "http://myserver.com/mysuperlargefile.pdf");
ifrm.style.width.value = "100px";
ifrm.style.height.value = "100px";
ifrm.onload = function(){ alert('hi'); };
$doc.body.appendChild(ifrm);
}-*/;



Thanks,

JP

sven
26 Mar 2009, 11:49 AM
could be also same domain policy. Try to upload your app to the same domain or the file to the domain your app is on

jpnet
26 Mar 2009, 11:53 AM
could be also same domain policy. Try to upload your app to the same domain or the file to the domain your app is on
Thanks for your quick responses. I really appreciate them.

It's not a same domain problem. Both are localhost by the way. The PDF file is loading just fine. It's that the javascript Alert doesn't run in JSNI but runs in my HTML example. In both examples the PDF file loads fine.

Other thoughts?

Thanks again for your help.

-JP

jpnet
26 Mar 2009, 12:42 PM
OK, this seems to be a browser issue. The example HTML code only works in FF3. I tested it in Safari4, Chrome, and IE7. It doesn't work in any of those.

I compiled the GWT code, and what do you know... it works in FF3 but not the rest. Interesting... so the onload event for an Iframe only works in FF3?

-JP