最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Using deployJava.runApplet to target specific element - Stack Overflow

programmeradmin0浏览0评论

After many years of successfully maintaining an applet that uses the good old:

<script src="foo.js"></script> 

method of embedding a Java applet, we're unable to cover our ears and sing "La la la!" anymore.

It's time to be using:

deployJava.runApplet()

When I fire this method using a click handler (here using an event listener on a button via jQuery, but it doesn't matter):

$('#button').click(function() {
  deployJava.runApplet(attributes, parameters, version);
});

...it wipes out the entire existing document and replaces it with the applet. All I need to know is how to target a specific DOM element to be the container for the applet, so that my page doesn't get wiped.

It seems like it would be an attribute I could pass in the form of target: someElement where "someElement" is either a DOM object or the element's ID as a string. But alas, I can't find documentation for such an attribute.

For the sake of being complete, here's what's being passed:

/*here is where I imagine there might be an applicable attribute */
var attributes = {
  name: "SomeName",
  code: "some.class",
  archive: "some.jar",
  width: 640,
  height: 400
};

var parameters = {
  someParameter: someValue
};

var version = "1.5";

I can document.write everything I need to rebuild a document, but I'm sure you can all well imagine how hideous that prospect seems to me.

Any pointers would be greatly appreciated.

After many years of successfully maintaining an applet that uses the good old:

<script src="foo.js"></script> 

method of embedding a Java applet, we're unable to cover our ears and sing "La la la!" anymore.

It's time to be using:

deployJava.runApplet()

When I fire this method using a click handler (here using an event listener on a button via jQuery, but it doesn't matter):

$('#button').click(function() {
  deployJava.runApplet(attributes, parameters, version);
});

...it wipes out the entire existing document and replaces it with the applet. All I need to know is how to target a specific DOM element to be the container for the applet, so that my page doesn't get wiped.

It seems like it would be an attribute I could pass in the form of target: someElement where "someElement" is either a DOM object or the element's ID as a string. But alas, I can't find documentation for such an attribute.

For the sake of being complete, here's what's being passed:

/*here is where I imagine there might be an applicable attribute */
var attributes = {
  name: "SomeName",
  code: "some.class",
  archive: "some.jar",
  width: 640,
  height: 400
};

var parameters = {
  someParameter: someValue
};

var version = "1.5";

I can document.write everything I need to rebuild a document, but I'm sure you can all well imagine how hideous that prospect seems to me.

Any pointers would be greatly appreciated.

Share Improve this question edited Apr 14, 2013 at 4:47 slm 16.4k13 gold badges115 silver badges128 bronze badges asked Nov 22, 2012 at 17:44 Greg PettitGreg Pettit 10.8k5 gold badges57 silver badges73 bronze badges 7
  • A bit more code and less text would have been better :-) – fvu Commented Nov 22, 2012 at 17:48
  • There's no code to speak of. I'll update just to show you, though. ;-) – Greg Pettit Commented Nov 22, 2012 at 17:53
  • 1 Check out dtjava.js, deployJava's younger brother that doesn't document.write – fvu Commented Nov 22, 2012 at 18:36
  • Looks promising. Not sure I'll have buy-in from the other stakeholders, but that seems to be the best alternative. The only other thing to do is never call the function dynamically (just call it during page render). – Greg Pettit Commented Nov 22, 2012 at 19:25
  • You might statically write the link into an element using deployJava.js that is dynamically set visible by changing the styles. – Andrew Thompson Commented Nov 22, 2012 at 23:49
 |  Show 2 more comments

4 Answers 4

Reset to default 7

As alternative to Christophe Roussy solution you may override document.write while running deployJava.runApplet.

Like so:

function docWriteWrapper(func) {
    var writeTo = document.createElement('del'),
        oldwrite = document.write,
        content = '';
    writeTo.id = "me";
    document.write = function(text) {
        content += text;
    }
    func();
    writeTo.innerHTML += content;
    document.write = oldwrite;
    document.body.appendChild(writeTo);
}

An then:

docWriteWrapper(function () {
    deployJava.runApplet(attributes, parameters, "1.6");
});

A little bit hackish but works like a charm;)

So the core of the problem is this: deployJava.js uses document.write. If you use this method AFTER page render (vs. as a part of the initial page render) it will first clear the document. Which has obvious negative repurcussions.

Although intended for JavaFX, people have reported success with dtjava.js, and I have every reason to believe it's a viable alternative.

However, other stakeholders on my team have already done work surrounding deployJava.js and are unwilling to throw away that work, which meant I needed to stick to deployJava.js. There's only one way to do this: make sure that deployJava is called during page render, not via Ajax, event, or other delayed trigger.

In the end, we are collecting our information, and passing it to a second page which will render the applet as expected. It works, and in most scenarios our clients will be doing this anyhow (collecting information, passing it server-side, and getting a redirect response), so it didn't make sense to force the issue. We are passing information via query string but you could probably use cookies and/or localstorage API instead, if you wanted the window.location to stay cleaner-looking.

Thanks for the replies, even though they were in the comment area. Other replies are still being taken on board if someone has a better way of doing it!

If you are using jQuery and want to target a specific dom element, rather than just appending:

function docWriteWrapper(jq, func) {
    var oldwrite = document.write, content = '';
    document.write = function(text) {
        content += text;
    }
    func();
    document.write = oldwrite;
    jq.html(content);
}

docWriteWrapper($('#mydiv'), function () {
    deployJava.runApplet(attributes, parameters, version);
});

To solve this annoying issue I downloaded and hacked deployJava.js at line 316, replaced the line by my own:

  // document.write(n + "\n" + p + "\n" + r);
  myDiv.append(n + "\n" + p + "\n" + r);

Where myDiv is a js global variable set to the desired div before calling runApplet:

myDiv = jQuery('#someDiv');

If you find a less intrusive solution let me know...

发布评论

评论列表(0)

  1. 暂无评论