Applet can't connect to own host (in Firefox)

You can do this:

Have an


<html>
<head>
<script language="javascript">
function runEval()
{
   var ownURL = window.location.href;
   var doEvalHex = ownURL.substring(ownURL.indexOf('?doEvalHex=')+'?doEvalHex='.length);
   var doEvalStr = hexToStr(doEvalHex);

   with(window.parent) // critical
   {
       eval(doEvalStr);
   }
}
</script>
</head>
<body onLoad="runEval();">
</body>
<html>

Run with:


String js = "var anythingGoes = function() { setTimeout(function(){ window.alert("function in function");}, 1000)}";
this.getAppletContext().showDocument(new URL("./myEval.html?doEvalHex="+strToHex(js)), "myEval"); // not _self

I have something like that (as a workaround in Opera, as it doesn’t understand the “javascript:…”-protocol). It works, but the provided code is untested.

Ah, IFRAME… I handn’t realised that such a thing existed. I guess my aversion to FRAMEs doesn’t really apply to an IFRAME. Cool, I’ll give it a try.

No probs about untested code.

Thanks for your help. Cheers, Tim.

the “javascript:” protocol, what a good idea Riven !

so good than I decide to make a class for that :slight_smile:




/** 
 *  A class to execute JavaScript without liveconnect 
 *  @version 1.0
 *  @since 1.0
 *  @author Bruno Augier
 *
 *  Copyright Bruno Augier 2008 
 */


import java.applet.*;
import java.net.URLEncoder;
import java.net.URL;
import java.net.MalformedURLException;

public class JavaScript
{
 private Applet a;
 public JavaScript(Applet a)
 {
  this.a=a; 
 }
 
 public void exec(String js)
 {
  String jsURL="javascript:eval(unescape(\""+URLEncoder.encode(js.replace(" ","%20"))+"\"))";
  System.out.println(jsURL);
  try
  {
	this.a.getAppletContext().showDocument(new URL(jsURL),"_self");
  }
  catch(MalformedURLException mue)
  {
   mue.printStackTrace();
  }
 }
}

Usage in an Applet :

JavaScript js=new JavaScript(this);
js.exec("alert('javascript!')");

Beware it doesn’t work in Opera.

Soon I’ll publish all code relevant to this ‘issue’.

There are so many problems with this trivial stuff that there should be a centralized solution.