LiveConnect applet crashing after page reload, please help

Discussion in 'Javascript' started by DKM, Jun 12, 2005.

  1. DKM

    DKM Guest

    Here are the source code files to a Java applet that utilizes
    LiveConnect to communicate with Javascript, and the HTML file.

    The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
    IE crashes when one refreshes the page or leave the page. This happens
    only after calling the Java method more than once. It does not crash if
    the Java method is called just once and then the page is refreshed.

    FireFox does not crash at all and no error whatsoever. However, it
    takes a good minute or two to display the newly created element. And,
    if during that time if one calls the Java method, it gives an error
    and it never works afterwords. But, once the Java method returns after
    the first time, it works robustly no matter how many times the Java
    method is called or if the page is refreshed or closed.

    Can any LiveConnect guru please take a look at this code and let me
    know whats wrong with it.

    Thank you very much in advance.

    D.K. Mishra

    ======== Hello.class ========
    import java.applet.Applet;
    import java.awt.Graphics;
    import netscape.javascript.*;

    public class Hello extends Applet {

    private JSObject win;
    private JSObject doc;

    public void init() {
    }

    public void start() {
    win = JSObject.getWindow(this);
    doc =(JSObject) win.getMember("document");
    }

    //A set of 2 overloaded helper methods to create object array to pass
    // as the 2nd argument to doc.call(string,Object[]).
    public Object[] objArr(JSObject jso) {
    Object[] ret = {jso};
    return ret;
    }
    public Object[] objArr(String str) {
    Object[] ret = {str};
    return ret;
    }

    //This cretes a filled HTML Tag like <p>Hello</p> or
    //<i>world!</i>.
    public JSObject createFilledTag(String strTag, String strText) {
    JSObject fragDoc = (JSObject)
    doc.call("createDocumentFragment",null);
    JSObject tagEle = (JSObject)
    doc.call("createElement",objArr(strTag));
    JSObject tagTextEle =
    (JSObject)doc.call("createTextNode",objArr(strText));
    tagEle.call("appendChild",objArr(tagTextEle));
    fragDoc.call("appendChild",objArr(tagEle));
    return fragDoc;
    }

    //This method is called from javascript. It inserts
    //*** Hello World! ***" into the empty <p id="para"></p>
    //element
    public void insertText(String str) {
    JSObject paraEle = (JSObject) doc.call("getElementById",
    objArr(str));
    JSObject tmpEle = createFilledTag("b","*** Hello World! ***");
    paraEle.call("appendChild",objArr(tmpEle));
    }
    }
    ======== hello.htm ========
    <html>
    <head>
    <title> New Document </title>
    <script>
    function addElement() {
    app = document.getElementById("Hello");
    app.insertText("para");
    }
    </script>
    </head>
    <body>
    <input type="button" onclick="addElement()" value="Fill"/>
    <p id="para"></p>
    <applet id="Hello" code="Hello.class" width="1" height="1"
    mayscript="true" scriptable="true">
    </applet>
    </body>
    </html>
     
    DKM, Jun 12, 2005
    #1
    1. Advertising

  2. DKM

    Roland Guest

    On 12-6-2005 4:02, DKM wrote:

    > Here are the source code files to a Java applet that utilizes
    > LiveConnect to communicate with Javascript, and the HTML file.
    >
    > The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
    > IE crashes when one refreshes the page or leave the page. This happens
    > only after calling the Java method more than once. It does not crash if
    > the Java method is called just once and then the page is refreshed.
    >
    > FireFox does not crash at all and no error whatsoever. However, it
    > takes a good minute or two to display the newly created element. And,
    > if during that time if one calls the Java method, it gives an error
    > and it never works afterwords. But, once the Java method returns after
    > the first time, it works robustly no matter how many times the Java
    > method is called or if the page is refreshed or closed.
    >
    > Can any LiveConnect guru please take a look at this code and let me
    > know whats wrong with it.
    >
    > Thank you very much in advance.
    >
    > D.K. Mishra
    >

    [snip]

    Your applet not only crashes in my IE6, but also in FF1.0.4, on various
    occasions: repeated pressing the Fill button, leaving page and going
    back (using browser's Forw & Back buttons).

    I thought that it could be caused by the applet holding on to the win
    and doc objects, but inlining the code of start() in the insertText and
    createFilledTag methods didn't make any difference.

    I guess that LiveConnect isn't as stable as it should be, unfortunately.
    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Jun 12, 2005
    #2
    1. Advertising

  3. DKM

    DKM Guest

    Roland wrote:
    > On 12-6-2005 4:02, DKM wrote:
    >
    > > Here are the source code files to a Java applet that utilizes
    > > LiveConnect to communicate with Javascript, and the HTML file.
    > >
    > > The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
    > > IE crashes when one refreshes the page or leave the page. This happens
    > > only after calling the Java method more than once. It does not crash if
    > > the Java method is called just once and then the page is refreshed.
    > >
    > > FireFox does not crash at all and no error whatsoever. However, it
    > > takes a good minute or two to display the newly created element. And,
    > > if during that time if one calls the Java method, it gives an error
    > > and it never works afterwords. But, once the Java method returns after
    > > the first time, it works robustly no matter how many times the Java
    > > method is called or if the page is refreshed or closed.
    > >
    > > Can any LiveConnect guru please take a look at this code and let me
    > > know whats wrong with it.
    > >
    > > Thank you very much in advance.
    > >
    > > D.K. Mishra
    > >

    > [snip]
    >
    > Your applet not only crashes in my IE6, but also in FF1.0.4, on various
    > occasions: repeated pressing the Fill button, leaving page and going
    > back (using browser's Forw & Back buttons).


    In FireFox, if you don't click 'Fill' till it displays "Hello World!"
    for the first time, it does not crash. Does it display "Hello World!"
    immidiately when you click Fill for the first time? In my case, it
    takes a good minute or two. After the first time you can click Fill as
    many times as you want and it does not crash.

    IE 6.0 displays "Hello World!" rightaway and keeps working till you
    reload the page when it will crash.

    I am still experimenting and will let you know if I succeed.

    Thank you for your time and help. I really appreciate all the help and
    tips. I am completely new to Java or any modern day COM programming.
    Given that all these technology are not new and have been around for a
    good decade, I thought there would be nice easy tools to design applets
    that can interact with the browser's DOM.

    Thank you again.

    D.K. Mishra


    >
    > I thought that it could be caused by the applet holding on to the win
    > and doc objects, but inlining the code of start() in the insertText and
    > createFilledTag methods didn't make any difference.
    >
    > I guess that LiveConnect isn't as stable as it should be, unfortunately.
    > --
    > Regards,
    >
    > Roland de Ruiter
    > ` ___ ___
    > `/__/ w_/ /__/
    > / \ /_/ / \
     
    DKM, Jun 12, 2005
    #3
  4. DKM

    Guest

    I have found inconsistent actions regarding the calling of start() and
    init() when a page containing an applet is reloaded. I used a kludge
    where I always dumped all my data and started completely clean when
    either were called.
     
    , Jun 13, 2005
    #4
  5. DKM

    DKM Guest

    Andrew Thompson wrote:
    > On 11 Jun 2005 19:02:19 -0700, DKM wrote:
    >
    > > Here are the source code files to a Java applet that utilizes
    > > LiveConnect to communicate with Javascript, and the HTML file.

    >
    > Two of the most important aspects of a correctly functioning
    > applet driven DHTML web-page are..
    >
    > - The HTML itself. If the HTML is not structurally
    > correct, a browser is free to interpret it any way
    > it likes/is best able[1]. You can validate your
    > HTML here <http://validator.w3.org/>..
    >
    > - The Javascript that adds the 'D' to the Dynamic HTML.
    > It needs to be as robust as practicable, and that usually
    > depends on a technique known as 'feature detection'.
    > More details in the JS shown here..
    > <http://www.jibbering.com/faq/#FAQ4_15>
    >
    > > <title> New Document </title>
    > > <script>

    >
    > [1] This is not valid HTML.


    I am totally at a loss. The whole thing is working if I use plain
    javascript to add dyanmic stuff by calling createElement, appendChild
    and such.

    Now, all I am trying to do is create the same from inside an applet.
    And, it works both in IE and Firefox, but as I pointed out IE crashes
    when the page is refreshed and FireFox will crash only on one condition
    and it can be prevented from crashing by setting a variable to false
    before calling the method and wait till it gets set to true from inside
    the applet.

    >
    > > function addElement() {
    > > app = document.getElementById("Hello");
    > > app.insertText("para");
    > > }
    > > </script>

    >
    > [2] ..and that not robust script.
    >
    > I suggest you strip all the Java out of it for the moment and
    > concentrate on getting the HTML and Javascript into a valid,
    > reliable form.


    I have done this. I started without Java stuff and have it working in
    both IE and FireFox.

    I have now found a solution for the problem for IE. That is I am no
    more creating dynamic elements inside the element. Instead, I am
    creating all the dynamic stuff in a string and passing the string to
    Javascript variable. Then from Javascript, I basically set the string
    to the innerHTML of a container tag. It works perfectly. Now, ofcourse
    the same does not work in FireFox. Its a XHTML file and the synamic
    stuff contains tags. FireFox displays the < as &lt; and > as &gt; and
    the content is displayed as is. For example it displays as
    &lt;b&gt;"Hello World!!!&lt;\b&gt; instead of displaying Hello World!!!
    in bold letters. I don't know how that can be taken cre of.

    Again, thanks very much in advance.

    D.K. Mishra




    >
    > I have set the follow-ups to c.l.js only, though you may need
    > to consult a group such as c.i.w.a.h.[3] for HTML advice.
    >
    > [3] <http://groups.google.com.au/group/comp.infosystems.www.authoring.html>
    >
    > 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
     
    DKM, Jun 15, 2005
    #5
  6. DKM

    DKM Guest

    wrote:
    > I have found inconsistent actions regarding the calling of start() and
    > init() when a page containing an applet is reloaded. I used a kludge
    > where I always dumped all my data and started completely clean when
    > either were called.


    I am not sure I understand what you are saying. I have nothing in
    init(). But, I have just the following in strat():

    win = (JSObject) JSObject.getWindow(this);

    What am I supposed to be doing in init(), start() and destroy()?

    Thanks very much in advance.

    D.K.Mishra
     
    DKM, Jun 15, 2005
    #6
  7. DKM

    DKM Guest

    DKM wrote:
    > Here are the source code files to a Java applet that utilizes
    > LiveConnect to communicate with Javascript, and the HTML file.
    >
    > The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
    > IE crashes when one refreshes the page or leave the page. This happens
    > only after calling the Java method more than once. It does not crash if
    > the Java method is called just once and then the page is refreshed.
    >
    > FireFox does not crash at all and no error whatsoever. However, it
    > takes a good minute or two to display the newly created element. And,
    > if during that time if one calls the Java method, it gives an error
    > and it never works afterwords. But, once the Java method returns after
    > the first time, it works robustly no matter how many times the Java
    > method is called or if the page is refreshed or closed.
    >
    > Can any LiveConnect guru please take a look at this code and let me
    > know whats wrong with it.
    >
    > Thank you very much in advance.
    >
    > D.K. Mishra
    >
    > ======== Hello.class ========
    > import java.applet.Applet;
    > import java.awt.Graphics;
    > import netscape.javascript.*;
    >
    > public class Hello extends Applet {
    >
    > private JSObject win;
    > private JSObject doc;
    >
    > public void init() {
    > }
    >
    > public void start() {
    > win = JSObject.getWindow(this);
    > doc =(JSObject) win.getMember("document");
    > }
    >
    > //A set of 2 overloaded helper methods to create object array to pass
    > // as the 2nd argument to doc.call(string,Object[]).
    > public Object[] objArr(JSObject jso) {
    > Object[] ret = {jso};
    > return ret;
    > }
    > public Object[] objArr(String str) {
    > Object[] ret = {str};
    > return ret;
    > }
    >
    > //This cretes a filled HTML Tag like <p>Hello</p> or
    > //<i>world!</i>.
    > public JSObject createFilledTag(String strTag, String strText) {
    > JSObject fragDoc = (JSObject)
    > doc.call("createDocumentFragment",null);
    > JSObject tagEle = (JSObject)
    > doc.call("createElement",objArr(strTag));
    > JSObject tagTextEle =
    > (JSObject)doc.call("createTextNode",objArr(strText));
    > tagEle.call("appendChild",objArr(tagTextEle));
    > fragDoc.call("appendChild",objArr(tagEle));
    > return fragDoc;
    > }
    >
    > //This method is called from javascript. It inserts
    > //*** Hello World! ***" into the empty <p id="para"></p>
    > //element
    > public void insertText(String str) {
    > JSObject paraEle = (JSObject) doc.call("getElementById",
    > objArr(str));
    > JSObject tmpEle = createFilledTag("b","*** Hello World! ***");
    > paraEle.call("appendChild",objArr(tmpEle));
    > }
    > }
    > ======== hello.htm ========
    > <html>
    > <head>
    > <title> New Document </title>
    > <script>
    > function addElement() {
    > app = document.getElementById("Hello");
    > app.insertText("para");
    > }
    > </script>
    > </head>
    > <body>
    > <input type="button" onclick="addElement()" value="Fill"/>
    > <p id="para"></p>
    > <applet id="Hello" code="Hello.class" width="1" height="1"
    > mayscript="true" scriptable="true">
    > </applet>
    > </body>
    > </html>



    I posted the above code at Sun's bug database and I have received a
    note that it is indeed a bug and they will try to fix it.

    Here is the link to the bug database site at Sun's web site:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6289379

    Thanks everyone.

    D.K. Mishra
     
    DKM, Jun 23, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DKM
    Replies:
    1
    Views:
    541
  2. DKM
    Replies:
    9
    Views:
    1,141
    Andrew Thompson
    Jun 24, 2005
  3. Richard Maher
    Replies:
    0
    Views:
    834
    Richard Maher
    Nov 20, 2010
  4. Richard Maher
    Replies:
    5
    Views:
    489
    Richard Maher
    Nov 23, 2010
  5. Replies:
    0
    Views:
    133
Loading...

Share This Page