Link to home
Start Free TrialLog in
Avatar of ptreves
ptreves

asked on

Unable to read Javascript from Java

Hello,

I am attempting to read the value of a flag variable written from a button onclick event in javascript from a Java Applet. I am making a JSObject call.

The JSObject call throws an JSException and does not return any values.

I write setter/getter methods in javascript to increment the value of variable PowerbarClicked each time a powerbar is clicked. I made sure to load the applet from javascript from an <OBJECT tag and specifying MAYSCRIPT.

Any ideas what could be the issue ?

Paolo
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves
ptreves

ASKER

Hello,

My javascript document is divided in 2 frames. A top frame called powerbar frame that contains 10 different buttons. One of the buttons called ActiveAlarm is itself an applet. The other 9 buttons are divided half and half in help buttons and normal buttons. One of the normal buttons is called the configuration button.

The ActiveAlarm button handles communication. When a timeout occurs a Faulty Communication Warning Dialog is displayed in the bottom frame called the display frame. The width, height, Xoffset and yoffset are set in javascript code and read through JSOject calls in the java applet code.

I want to replicate the same functionality from the configuration button which is programmed as a normal button (not an applet). My JSObject calls return no values.

What could be the issue ?

Paolo

PS: I am always using INternet Explorer under Windows 7.
Sorry, I'm not quite clear.
Do you mean that you do have working interaction between javascript and the applet in your browser for some
case, but it is not working for another case?
Avatar of ptreves

ASKER

Yes, that is exactly my situation.
The ActiveAlarm button Applet JSObject calls work.
From the configuration normal button, JSObject calls don't work.

Paolo
Sorry, I'm still trying to understand.

When you are writing

>One of the buttons called ActiveAlarm is itself an applet.

do you mean that you have an applet and within the applet you have the button.
So this applet indeed can access the elements of the web page which are not
part of this applet but some general HTML say text elements or anything.

Well, if this is correct, it is understandable.

Then what you mean by "the configuration is a normal button" -  just plain HTML button.
So then what do you want from this button then?

I'm sorry, I'm still missing something





 
Avatar of ptreves

ASKER

The javascript document (or web-page) is divided in 2 frames, 1 top frame called a powerbar frame with about 10 buttons. The rest of the document or web page is another frame called the displayframe.

The powerbar buttons are divided in 3 categories.
1 category numbers only 1 powerbar button which is called the ActiveAlarm button and is basically a Java class that extends JApplet. The other 9 powerbar buttons are divided in 2 other groups, 1 group called the normal buttons and the last group called the popup buttons. The popup buttons open another browser window whcih basically contains row sets of data.

The normal buttons are programmed in javascript and have events associated to them. These events call Java applets on the displayframe.

One of the normal buttons is called the Configuration button.
I am trying to make JSObject calls to read a flag that would let me know if any of the powerbar buttons was clicked thsi in turn would allow me to call a JOptionPane on the active applet in the display frame.

The problem is that the JSObject calls I make don't return anything.
They work for the ActiveAlarm Applet button but not for the Configuration normal button.

Suggestions ?

Paolo
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves

ASKER

The ActiveAlarm button is itself an Applet (a Java class that extends JApplet).
The ActiveAlarm button is responsible for monitoring communications with an external IO device. If a communication problem occurs a JDialog is displayed in the displayframe. The width, height, Xoffset and YOffset of the JDialog get their values from JSObject calls to coresponding javascript functions.

I would like to do read a flag called PowerbarClicked from the configuration button (which is a NORMAL button) and if this flag is set display a JOptionPane to prompt the user.

All my attempts to read the PowerbarClicked variable using JSObject calls do not return anything (no object is returned).

The JDialog parameters associated JSObject calls return JSObject which is converted to a string and then parsed to a Float or Integer.

Paolo

 Your PowerbarClicked variable exists in the upper frame and your applet, which gets loaded when you press configuration button  
is coming from the page in the lower frame - is this my udnderstanding correct?
Avatar of ptreves

ASKER

yes, that is correct. PowerbarClicked is written in javascript. The applet is written in java.
But one additional issue is that your oowerbarclicked script is not on the same web page from where your applet with which you want to communoicate is loaded.

Post your code in the applet throuigh which you try to communicate with the web page
Also poost your code from the ActiveAlarm code  where it successfuly communicastes (as it is coming from the same page). Maybe we stil could think of simething
 - myabe there is a way of communicating between two pages in the different frames
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves

ASKER

Here is the Java Applet methods that set the size and position of the JDialog based on javascript parameters read through JSObject calls:

    /**
     * Returns the size of the dialog.
     *
     * @param parameters parameters for the js call
     * @return dialog size
     */
    private Dimension getDialogSize(String[] parameters) {
        Dimension size = null;

        Object width = null;
        Object height = null;
        //Object clicks = null;

        //try {
           if (isRunningInBrowser()) {
               //width = this.getJSObject().call("getWidth", parameters);
               //height = this.getJSObject().call("getHeight", parameters);
               //clicks = this.getJSObject().call("getPowerbarClicked", parameters);
               width = this.getJSObject().call("getWidth", null);
               height = this.getJSObject().call("getHeight", null);
               //clicks = this.getJSObject().call("getPowerbarClicked", null);
           }
        //} catch (JSException jse) {
              //System.out.println("JSException method getJSObject from ActiveAlarmButton");
        //}
       
        if (width != null && height != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Got size from browser; width = " + width + ", height = " + height);
            }

            // FF reports them with decimal points
            try {
                float newWidth = Float.parseFloat(width.toString());
                float newHeight = Float.parseFloat(height.toString());
                //float newClicks = Float.parseFloat(clicks.toString());
                //int newiClicks = (int) newClicks;
               
                newWidth = newWidth / 2;
                newHeight = newHeight / 2;
                size = new Dimension((int) newWidth, (int) newHeight);

                //System.out.println("***********************");
                //System.out.println("Powerbar clicked: %d \n" + newiClicks);
                //System.out.println("***********************");

                //JOptionPane.showMessageDialog(null, "Dialog width: %d; Powerbar clicked: %d times." + newWidth + newiClicks);
            } catch (NumberFormatException e) {
                // do nothing if the browser reports it incorrectly
                  e.printStackTrace();
            }
        }

        return size;
    }

    /**
     * Returns the location of the dialog.
     *
     * @param parameters parameters for the js call
     * @return dialog location
     */
    private Point getDialogLocation(String[] parameters) {
        Point location = null;

        Object offsetX = null;
        Object offsetY = null;

        if (isRunningInBrowser()) {
            offsetX = getJSObject().call("getOffsetX", parameters);
            offsetY = getJSObject().call("getOffsetY", parameters);
        }

        if (offsetX != null && offsetY != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Got offset from browser; offsetX = " + offsetX + ", offsetY = " + offsetY);
            }

            // FF reports them with decimal points
            try {
                float newOffsetX = Float.parseFloat(offsetX.toString());
                float newOffsetY = Float.parseFloat(offsetY.toString());

                if (newOffsetX >= 0 && newOffsetY >= 0) {
                    location = new Point((int) newOffsetX, (int) newOffsetY);
                }
            } catch (NumberFormatException e) {
                // do nothing if the browser reports it incorrectly    
            }
        }

        return location;
    }
Avatar of ptreves

ASKER

Here is the OBJECT tage for the ActiveAlarm button (which is itself an applet):

        if($TypeOfButton[$Button] eq "ActiveAlarms")
        {
            print "<span style='border-right: #3A73BA 2px solid;'>";
            print "<!--[if !IE]> Firefox and others will use outer object -->";
            print "<object id='activeAlarmButtonApplet' classid='java:com.gepower.d400.view.runtime.ActiveAlarmButton.class'";
            print "        type='application/x-java-applet'";
            print "        height='38' width='37.5' align='absbottom'>";
            print "     <param name ='java_arguments' value='-XX:ThreadStackSize=512' />";
            print "     <param name='code' value='com.gepower.d400.view.runtime.ActiveAlarmButton.class' />";
            print "     <param name='codebase' value='../d400/' />";
            print "     <param name='name' value='PowerBarButton' />";
            print "     <param name='mayscript' value='true' />";
            print "     <param name='type' value='application/x-java-applet;version=1.5' />";
            print "     <param name='location' value='/cgi-push/PowerBarAlarm.cgi' />";
            print "     <param name='toolTip' value='$ToolTip[$Button]' />";
            print "     <param name='authCookie' value='$cookie' />";
            print "     <param name='displayUrl' value='$actualUrl' />";
            print "     <param name='sessID' value='$sessionKey' />";
            print "     <param name='logoutUrl' value='$logoutUrl' />";
            print "     <param name = 'cache_archive' value='$jars' />";
            print "     <param name = 'cache_version' value='$versions' />";
            print "     <param name = 'device_type' value='$device_type' />";            
            print "<!--<![endif]-->";
Avatar of ptreves

ASKER

This the code for a Configuration (normal button):

        elsif($userLevel ne "$HmiAccessManager::HMI_SUPERVISOR")
        {
            if ($TypeOfButton[$Button] ne "Configuration")
            {
                printNormalButton($ToolTip[$Button], $displayUrl, $Image[$Button], $TypeOfButton[$Button]);
            }
        }                        
        else
        {
            printNormalButton($ToolTip[$Button], $displayUrl, $Image[$Button], $TypeOfButton[$Button]);
        }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves

ASKER

I think you might have an interesting point.
The ActiveAlarm button is programmed is Java as an Applet and exists on the top level frame called the powerbar frame. It's JSObject calls are calling javascript methods in the same frame.
I could try something like:

width = this.getJSObject().call("parent.powerbarframe.getWidth", null);
height = this.getJSObject().call("parent.powerbarframe.getHeight", null);
powerbarClick = this.getJSObject().call("parent.powerbarframe.getPowerbarClick", null);

My configuration applet being called from the bottom frame: parent.displayframe.
Avatar of ptreves

ASKER

I wrote a method from the ConfigurationController that manages applets in the bottom displayframe
The method is called getJSValues:

    private void getJSValues() {
        Object width = null;
        Object height = null;

        try {
               width = currentApplet.getJSObject().call("parent.powerframe.getWidth", null);
               height = currentApplet.getJSObject().call("parent.powerframe.getHeight", null);
        } catch (JSException jse) {
              jse.printStackTrace();
        }
       
        if (width != null && height != null) {
            try {
                float newWidth = Float.parseFloat(width.toString());
                float newHeight = Float.parseFloat(height.toString());

                System.out.println("*********************************************************");
                System.out.println("Powerbar clicked: %d, width: %d, height: %d \n" + newWidth + newHeight);
                System.out.println("*********************************************************");
            } catch (NumberFormatException e) {
                  e.printStackTrace();
            }
        }
    }
Avatar of ptreves

ASKER

When executing the code, I get the following Stack Trace in the Java Console Window:

netscape.javascript.JSException
      at netscape.javascript.JSObject.getWindow(Unknown Source)
      at com.gepower.d400.view.D400Applet.getJSObject(D400Applet.java:395)
      at com.gepower.d400.view.config.ConfigurationController.getJSValues(ConfigurationController.java:661)
      at com.gepower.d400.view.config.ConfigurationController.powerbarClick(ConfigurationController.java:441)
      at com.gepower.d400.view.config.ConfigurationController.shutdown(ConfigurationController.java:311)
      at com.gepower.d400.view.LifeCycleControllerApplet.destroy(LifeCycleControllerApplet.java:62)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
      at java.applet.Applet.getAppletContext(Unknown Source)
      ... 8 more
JSException method getJSObject from ConfigurationController
Avatar of ptreves

ASKER

netscape.javascript.JSException
      at netscape.javascript.JSObject.getWindow(Unknown Source)

It is not finding the parent.powerframe window ?
What is the cause of this Exception ?

Avatar of ptreves

ASKER

Maybe I am not calling my powerframe. Should I use some call similar to this javascript method:

function setHeight(){
    if (window.mozInnerScreenY === undefined) {
        // internet explorer
        height = document.body.clientHeight + parent.displayframe.document.body.clientHeight;
    } else {
        // firefox
        height = top.innerHeight;
    }
}
Avatar of ptreves

ASKER

I noticed this entry in my Stack Trace:

network: Cache entry not found [url: http://3.94.247.54/jsobject.jar, version: null]

Could this have anything to do with it ?
Avatar of ptreves

ASKER

Here are other entries of the Stack Trace:

network: Cache entry found [url: http://3.94.247.54/jsobject.jar, version: 2011.9.27.3546] prevalidated=false/0
cache: Reading Signers from 1000 http://3.94.247.54/jsobject.jar | D:\Users\502001950\AppData\LocalLow\Sun\Java\Deployment\cache\6.0\27\55532c9b-7447a742-2011.9.27.3546-.idx
cache:  Read manifest for http://3.94.247.54/jsobject.jar: read=236 full=236
network: Cache entry not found [url: http://3.94.247.54/jsobject.jar, version: null]
Are your lower frame and upperframe coming from the same domain ?
Avatar of ptreves

ASKER

Take a look at this:

<frameset rows="47,0,*" border="0">
  <Frame name="powerframe" src="../cgi-bin/hmipowerbar.cgi" scrolling="no" noresize="noresize" onload="setTimeout(powerframeLoaded, 0)"/>
  <Frame name="JSCalls" src="blank.html" scrolling="no" noresize="noresize"/>
  <Frame name="displayframe" src="$homePageURL" onload="setTimeout(displayframeLoaded, 0)"/>
</frameset>
Avatar of ptreves

ASKER

Also, take a look at this:

        <object id='config' classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93'
            codebase='https://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab'
            height='99%' width='100%'>
            <param name ='java_arguments' value='-XX:ThreadStackSize=512' />
            <param name='code' value='com.gepower.d400.view.config.ConfigurationApplet.class' />
            <param name='codebase' value='../d400/' />
            <param name='mayscript' value='true' />
            <param name='type' value='application/x-java-applet;version=1.5' />
            <param name='authCookie' value='$cookie' />
            <param name='userName' value='$userName' />
            <param name='sessID' value='$sessionKey' />
            <param name = 'cache_option' value='Plugin' />
            <param name = 'cache_archive' value='$jars' />
            <param name = 'cache_version' value='$versions' />
            <param name ='device_type' value='$device_type' />
        </object>

We don't know what is
$homePageURL

but I assume that it is the same domain

Especially you are actully using frameset (i was for some reason thinking in the sense of iFrame).

With frameset in order to reach right frame, you proabbly can also try top.powerframe.getWidth...

Another option to try would be to add a javscript method to your displayfFrame, which would be calling
this method from top.powerframe
so that Applet will call method form its own frame

I think ithese interactions within javaScript should be much more ofetn used
than when applet reaches out to another frame,
so they are more likely free of bugs seems to me than interactions between applet and the web page
which are less often used.

Avatar of ptreves

ASKER

Here is a method I wrote:

    private void getJSValues() {
        Object width = null;
        Object height = null;
        Object clicks = null;
        Object clicks2 = null;

        System.out.println("getJSValues - inside method");
        try {
           //if (currentApplet.isRunningInLocalBrowserHMI()) {
               System.out.println("getJSValues - running in Browser");
               //width = currentApplet.getJSObject().call("parent.powerframe.getWidth", null);
               //height = currentApplet.getJSObject().call("parent.powerframe.getHeight", null);
               width = currentApplet.getJSObject().call("top.powerframe.getWidth", null);
               height = currentApplet.getJSObject().call("top.powerframe.getHeight", null);
               clicks = currentApplet.getJSObject().call("parent.frame[0].powerbarClicked", null);
               clicks2 = currentApplet.getJSObject().call("parent.powerframe.powerbarClicked", null);
               System.out.println("getJSValues - get width/height");
           //}
        } catch (JSException jse) {
              jse.printStackTrace();
              System.out.println("JSException method getJSObject from ConfigurationController");
        }
       
        if (width != null && height != null) {
            // FF reports them with decimal points
            try {
                float newWidth = Float.parseFloat(width.toString());
                float newHeight = Float.parseFloat(height.toString());
                float newClicks = Float.parseFloat(clicks.toString());
                float newClicks2 = Float.parseFloat(clicks2.toString());
                int newiClicks = (int) newClicks;
                int newiClicks2 = (int) newClicks2;
               
                newWidth = newWidth / 2;
                newHeight = newHeight / 2;

                System.out.println("**********************************************************************************");
                System.out.println("Powerbar width: %d, height: %d \n" + newWidth + newHeight);
                System.out.println("Powerbar clicks: %d,clicks2: %d \n" + newiClicks + newiClicks2);
                System.out.println("**********************************************************************************");
            } catch (NumberFormatException e) {
                // do nothing if the browser reports it incorrectly
                  e.printStackTrace();
            }
        }
    }
Avatar of ptreves

ASKER

This is the stack trace in the Java Console:

getJSValues - inside method
getJSValues - running in Browser
netscape.javascript.JSException
      at netscape.javascript.JSObject.getWindow(Unknown Source)
      at com.gepower.d400.view.D400Applet.getJSObject(D400Applet.java:395)
      at com.gepower.d400.view.config.ConfigurationController.getJSValues(ConfigurationController.java:665)
      at com.gepower.d400.view.config.ConfigurationController.powerbarClick(ConfigurationController.java:429)
      at com.gepower.d400.view.config.ConfigurationController.shutdown(ConfigurationController.java:311)
      at com.gepower.d400.view.LifeCycleControllerApplet.destroy(LifeCycleControllerApplet.java:62)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
      at java.applet.Applet.getAppletContext(Unknown Source)
      ... 8 more
JSException method getJSObject from ConfigurationController
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves

ASKER

>getJSValues - running in Browser

waht does it mean?

Just a comment to the Java Console.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptreves

ASKER

Thanks for all your help.
I was able to get this working using a BrowserContext class.

Regards,

Paolo
Avatar of ptreves

ASKER

The comments guided me towards a solution.