Re: LiveConnect applet crashing after page reload, please help





Andrew Thompson wrote:
> On 11 Jun 2005 19:02:19 -0700, DKM wrote:
>
> [ Note: Follow-ups set to comp.lang.java.programmer ]
>
> > The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
>
> I have a stable variant that I tried to add to add
> your bug report at Sun..
> <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6289379>
>
> Most of text disappeared, so I will try posting it here..
>
> ....
> Try this as a potential 'workaround'.

I had found a different workaround. That involved creating the html
string inside the applet and returning that string to javascript. The
javascript then will either use innerHTML (in IE 6.0) or the DOMParser
in Firefox to insert the html string in the proper place. And, this has
been working without any crash in IE or FireFox.

Your example below uses the eval method which seems to be the only
reliable method in JSObject. Also, your eval is calling the setPara in
javascript which is doing the actual insertion. So, basically, in your
example, instead of returning the html string to a javastring method to
set the innerHTML (as I am doing as a work around), you are calling
eval to call the javastring method with the appropriate id and html
string to set the innerHTML. I think my workaround of passing the html
string to the javascript method is a bit less convoluted.

I tested your below example, and it works without crashing in both IE
and FireFox.

Thank you.

D.K. Mishra


>
> I put '' because it takes a significantly
> different approach - performing the bulk
> of the element manipultion using JS functions
> specially developed for X-browser compatibility.
>
> This example fails when the user specifies
> an element id that does not exist.
>
> Developing a more robust script to account
> for those sort of errors is left as an
> exercise for the reader.
>
> See the sources for further details and notes.
>
> <sscce>
> import java.applet.Applet;
> import java.awt.*;
> import java.awt.event.*;
> import netscape.javascript.*;
>
> public class Hello extends Applet implements ActionListener {
>
> private JSObject win;
>
> TextField element;
> TextField text;
>
> public void init() {
> setLayout( new GridLayout(0,1) );
> add(new Label("Element"));
> element = new TextField("para");
> add( element );
>
> add(new Label("Text"));
> text = new TextField(", some <em>new</em> text");
> add( text );
>
> Button btn = new Button("Add Text");
> btn.addActionListener(this);
> add( btn );
> }
>
> public void actionPerformed(ActionEvent ae) {
> setPara();
> }
>
> public void start() {
> win = JSObject.getWindow(this);
> }
>
> /** Calls a JS function that changes named
> elements in the current page. */
> public void setPara() {
> try {
> // all sorts of things can go wrong here..
> win.eval( "setPara('" + element.getText() +
> "', '" + text.getText() + "')" );
> } catch (Throwable t) {
> // ..let's find out what.
> System.out.println( "There was a problem, " +
> "see stacktrace for further details" );
> t.printStackTrace();
> }
> }
> }
> </sscce>
>
> ---- index.html ----
> <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
> <!--
> It is better to specify HTML 4.01 (Strict), but this page includes the
> <applet> element, which is deprecated in HTML 4.
> -->
> <head>
> <meta http-equiv="CONTENT-TYPE" content="text/html; charset=iso-8859-1">
> <!--
> From a bug report at Sun - Bug Id.: 6289379
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6289379
> LiveConnect Applet crashes on page reload
> from a thread on comp.lang.java.help,
> Msg Id.: 1118541739.318472.180580@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> http://groups-beta.google.com/group/comp.lang.java.help/msg/35ddca8a2249914a
>
> This version incorporates the getElementWithId(id) function shown here..
> http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html#getEl
> to deal with browser compatibility issues.
> -->
> <title>Document manipulation with Javascript/LiveConnect</title>
> <script type='text/javascript' SRC='script.js'>
> </script>
> </head>
> <!-- Calls the JS function to locate the applet only after the
> onload* event has fired. * Indicating that the UA will now
> have identified all the elements within the page.
> -->
> <body onload='getApp();'>
> <div>
> <input type="button" onclick="addElement()" value="Fill"/>
> </div>
> <!--
> http://java.sun.com/products/plugin/1.3/docs/jsobject.html
> ..lists the <applet> format of the 'mayscript' attribute as below.
>
> The form used in the original page is refered to on that same
> page as being appropriate for the <embed> structure only.
> Neither the <embed> structure nor the MAYSCRIPT attribute
> where ever part of any W3C recommendation.
>
> Note that <applet> was part of HTML 3.2 but is deprecated in HTML 4.0.
>
> I feel the <applet> element is too important and useful to lose
> and should be *un*deprecated. But that is another matter.
> -->
> <applet id="Hello" code="Hello.class" width="100" height="100" MAYSCRIPT>
> </applet>
> <!-- These is our target elements, can they be altered reliably? -->
> <p id="para">The &lt;P&gt; element (id="para"). Some text</p>
> <div id="adiv">The &lt;DIV&gt; element (id="adiv"). Some text</div>
>
> </body>
> </html>
> ---- end index.html ----
>
> ---- script.js ----
> var app;
>
> /* Calls an x-browser script* developed for inclusion
> in the comp.lang.javascript FAQ. * One of several
> dealing with document manipulation. */
> function getApp() {
> app = getElementWithId("Hello");
> }
>
> /* Allows the HTML button to invoke the applet's 'setPara()
> method. The applet's setPara() method, in turn, invokes the
> (JS) setPara(id, txt) function using the current values of
> the text fields in the applet. */
> function addElement() {
> app.setPara();
> }
>
> /* The method that changes the element.
> Called by both the HTML button and the applet. */
> function setPara(id, txt) {
> document.getElementById(id).innerHTML =
> document.getElementById(id).innerHTML + txt;
> }
>
> /* Script obtained from ..
> http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html#getEl
> See the URL for further discussion. */
> function getElementWithId(id){
> var obj = null;
> if(document.getElementById){
> /* Prefer the widely supported W3C DOM method, if
> available:-
> */
> obj = document.getElementById(id);
> }else if(document.all){
> /* Branch to use document.all on document.all only
> browsers. Requires that IDs are unique to the page
> and do not coincide with NAME attributes on other
> elements:-
> */
> obj = document.all[id];
> }
> /* If no appropriate element retrieval mechanism exists on
> this browser this function always returns null:-
> */
> return obj;
> }
> ---- end script.js ----
>
> HTH
>
> --
> Andrew Thompson
> http://www.PhySci.org/codes/ Web & IT Help
> http://www.PhySci.org/ Open-source software suite
> http://www.1point1C.org/ Science & Technology
> http://www.LensEscapes.com/ Images that escape the mundane

.


Quantcast