Eliminate “Click to Activate”

I recently updated the web pages for my Java applets so that you don’t need to click to activate an applet before using it. You can go here to read the gory details about why most browsers now force you to click to activate controls and applets. I knew that it was possible to work around the problem, but when I went looking for a quick answer on the Web I was surprised at how hard it was to find a simple explanation of the problem and a concise example that demonstrated how to solve it. I hope that this post will help fill that gap.

JavaScript: The Definitive Guide

To avoid forcing your users to click to activate a control or applet, you need to emit the ‘object’, ‘embed’ or ‘applet’ tag via JavaScript using a function that is declared in a separate file. The following example shows how to change the HTML for an ‘applet’ tag, but the technique works just as well for the ‘object’ and ‘embed’ tags. If your ‘applet’ tag looked like this:

<applet code="MyApplet.class" width="100" height="200">
<param name="foo" value="bar">
</applet>

you would change it to this:

<script type="text/javascript"
        src="/clickToActivateWorkaround.js"></script>
<script type="text/javascript"><!--
clickToActivateWorkaround(
'<applet code="MyApplet.class" width="100" height="200">\\n');
//--></script>
<param name="foo" value="bar">
</applet>

Then create a new file called clickToActivateWorkaround.js in your web site’s root folder. The new file should contain the following single function:

function clickToActivateWorkaround(str)
{
        document.write(str);
}

That’s all you need to do!

One thought on “Eliminate “Click to Activate”

Leave a Reply