1

I have an Applet program, which works fine in Eclipse. I want to load this applet when I click on a button action from a html code. I have kept the html file in the same path where I have this applet class. I am just testing now, so haven't hosted this html file in server, before that I want to test this is working.

I searched forums and trying like below, but I couldn't get it working as expected. Applet is not launching on click button event. I am missing something, could someone guide me to make this working?

<HTML>
<HEAD>
    <TITLE> Welcome on board! </TITLE>
    <script>
        function loadApplet()
        {
            var appletbox=document.getElementById('MyApplet.class');

        }
    </script>
</HEAD>
<BODY>
    <p>Click here for new Applet session!</p>
    <button onclick="loadApplet">Click here</button>

</BODY>

Stella
  • 1,728
  • 5
  • 41
  • 95

4 Answers4

1

I think this will work

<HTML>
<HEAD>
    <TITLE> Welcome on board! </TITLE>
    <script type="text/javascript">
        function loadApplet()
        {
        alert("Load Applet");
        document.write("<applet code='MyApplet' height='300' width='300'></applet>");
        }
    </script>
</HEAD>
<BODY>
    <p>Click here for new Applet session!</p>
    <button onclick="loadApplet();">Click here</button>

</BODY>
</HTML>

I have tested on my machine and it is working

Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
  • Yes, Its working thank you. Can I open the applet in new window instead of running on the same web page? – Stella Mar 04 '14 at 10:21
  • I think it is not possible, but you can search for it. And mark my answer as Accpted, when you get the time – Mohammad Ashfaq Mar 04 '14 at 10:33
  • I see document.write is for writing string based on this reference http://javascript.info/tutorial/document-write But, I want to applet to be running when clicking on "Click here" button, as the Applet does different functionality later like connecting with another socket program etc. – Stella Mar 04 '14 at 12:38
  • Applet functionality is independent of What you are writing in document.write. – Mohammad Ashfaq Mar 04 '14 at 12:50
0

Try to add () for your function here, so you can use:

<button onclick="loadApplet()">Click here</button>

instead of:

<button onclick="loadApplet">Click here</button>
Felix
  • 37,892
  • 8
  • 43
  • 55
0

You forgot to add brackets

Try this:

 <button onclick="loadApplet()">Click here</button>
0

You are including your applet in wrong way. Check my answer on other question. You should use <applet>, <object> or <embed> tag to include applet on your page. This starts applet automatically, but when you use my method you can append applet to page clicking the button. In function loadApplet use code similar to my code.

On your page there is no element with id MyApplet.class, so you can't get this element by id.

Community
  • 1
  • 1
Tomasz Dzięcielewski
  • 3,829
  • 4
  • 33
  • 47