0

I have an applet and an iframe within the same page. The iframe tag has an onload attribute which calls a method in the applet every time the iframe page is changed. The applet needs to access the elements of the iframe document and count the number of different types of tag on the page.

I have the following code in the applet, which accesses the iframe

Class c = Class.forName("com.sun.java.browser.plugin2.DOM");
Method m = c.getMethod("getDocument", new Class[]{java.applet.Applet.class});
Object obj = m.invoke(null, new Object[]{this});
HTMLDocument doc = (HTMLDocument)obj;

NodeList nodeList = document.getElementsByName("iframe");
if (nodeList.getLength()>0) {
    Node node1 = nodeList.item(0);
    if (node1 instanceof HTMLIFrameElement) {
        Document doc = ((HTMLIFrameElement)node1).getContentDocument();
            if (doc instanceof HTMLDocument) {
                document = (HTMLDocument)doc;
                if (document.hasChildNodes() {
                    // do some stuff
                }
            }
        }
    }
}

If this script is run when the page first loads, it works fine - it accesses the code of the iframe and can access each element within the page.

But when the applet code is invoked from the onload attribute of the iframe, the applet can't read the document properly. It throws the following exception:

netscape.javascript.JSException: No such property "hasChildNodes" on JavaScript object

I'm accessing exactly the same document both times, so why does it fail when I use onload? Is there any way I can get around this?

claire
  • 93
  • 6

2 Answers2

0

JavaScipt can't access the iframe's DOM as it is on a separate server, but the applet can...

In think Andrew had the right idea. JS can access content on other servers using JSONP.

To communicate with cross-domain iframes, you may need to look into something like Porthole as well.

Community
  • 1
  • 1
Michael Szczepaniak
  • 1,970
  • 26
  • 35
0
  1. Figure how to do it in JavaScript
  2. Encapsulate that in a JS function.
  3. Call the function from the applet.

That way, at least it is easier to debug the JS and or JS/applet interaction separately.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • JavaScipt can't access the iframe's DOM as it is on a separate server, but the applet can, which is why I'm doing it this way. As far as I can tell, there's no other way of finding out the content of the iframe... that's why it's pretty annoying that I've hit a brick wall when it comes to reloading the page! – claire Feb 13 '12 at 23:50
  • Everything that Java does with a web page is basically handled by JS behind the scenes in any case. As such, if JS can't do it, I'd be astonished if an applet could. – Andrew Thompson Feb 13 '12 at 23:56
  • This baffled me too - but the applet could definitely access the iframe even though it shouldn't... – claire Feb 14 '12 at 00:06