4

Put a TMemo, a TEmbeddedWB and a TButton on a Delphi VCL form.

This is the code from the form unit:

procedure TForm1.Button1Click(Sender: TObject);
var
  vResult: OleVariant;
  Para1: string;
begin
  Para1 := '5'; // edPara.Text;
  vResult := EmbeddedWB1.ExecScriptEx('evaluate', [Para1]);
  ShowMessage('Result from the Script: ' + IntToStr(vResult));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  EmbeddedWB1.HTMLCode.Assign(Memo1.Lines);
end;

This is the content of the Memo1.Lines:

<HTML> 
<HEAD> 
<TITLE>Test Script</TITLE> 
<SCRIPT> 
  function evaluate(x) { alert("Hello from the script evaluate(x)"); return eval(x * x); } 
</SCRIPT> 
</HEAD> 
<BODY> TEST Script: eval(x * x)</BODY> 
</HTML>

But it does not work: vResult is 0 after clicking the button.

Why it does not work?

MartynA
  • 30,454
  • 4
  • 32
  • 73
user1580348
  • 5,721
  • 4
  • 43
  • 105

1 Answers1

3

The parameter type passed to ExecScriptEx must be an Integer in this case, not a string:

procedure TForm1.Button1Click(Sender: TObject);
var
  vResult: OleVariant;
  Para1: string;
  ParaInt: Integer;
begin
  //Para1 := '5'; // edPara.Text;
  ParaInt := 5;
  vResult := EmbeddedWB1.ExecScriptEx('evaluate', [ParaInt]);
  ShowMessage('Result from the Script: ' + IntToStr(vResult));
end;

Now it works!

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • Interesting, because when directly using JavaScript, you are in fact able to pass and use either/or. Except I wonder if the reason is because `'5' * '5'` isn't valid... – Jerry Dodge Aug 22 '15 at 15:09
  • @Jerry, yes, but you cannot multiply strings. – TLama Aug 22 '15 at 15:11
  • 1
    @TLama Yes, I just caught that. I was paying attention to the `alert()` before I noticed the `return()` calculation. I need to finish my coffee :-/ – Jerry Dodge Aug 22 '15 at 15:11