Evaluates JavaScript code and returns a String result.
The JavaScript code to evaluate.
Evaluates JavaScript code and returns a String result.
Visual Basic |
---|
Public Function Eval( _ ByVal Code As String _ ) As String |
The JavaScript code to evaluate.
The string result of the evaluation.
The following code is used to stringify the evaluation result:
V8 Value Stringify for Eval |
Copy Code |
---|---|
std::wstring V8ValueToString(const CefRefPtr<CefV8Value>& value, const CefRefPtr<CefV8Exception>& exception, std::wstring string_for_null, std::wstring string_for_undefined) { std::wstring result; if (exception.get() && !exception->GetMessageW().empty()) { result = L"__V8_EXCEPTION:" + std::wstring(exception->GetMessageW()); } else { if (value->IsNull()) { result = string_for_null; } else if (value->IsUndefined()) { result = string_for_undefined; } else if (value->IsBool()) { result = value->GetBoolValue() ? L"true" : L"false"; } else if (value->IsUInt()) { result = std::to_wstring(value->GetUIntValue()); } else if (value->IsInt()) { result = std::to_wstring(value->GetIntValue()); } else if (value->IsDouble()) { result = std::to_wstring(value->GetDoubleValue()); } else if (value->IsDate()) { CefTime t = value->GetDateValue(); wchar_t buffer[100]; wsprintf(buffer, L"d-d-d dd", t.year, t.month, t.day_of_month, t.hour, t.minute, t.second); result = buffer; } else if (!value->IsString()) { CComVariant v = V8ValueToVariant(value); v.ChangeType(VT_BSTR); CefRefPtr<CefV8Value> vv = VariantToV8Value(v); result = vv->GetStringValue().ToWString(); } else { result = value->GetStringValue().ToWString(); } } return result; } |
For programming languages that support OLE/COM Variant is recommended to use CallByName method instead of Eval.
You can control the returned value for null and undefined values by setting Settings.string_for_null and Settings.string_for_undefined properties during OnCreate event.
V8 JavaScript Evaluation Exceptions are converted to OLE/COM Automation Exceptions.
Private Sub mnuEval_Click() On Error Resume Next Dim s As String s = InputBox("JavaScript") If s = "" Then Exit Sub s = WebKitX1.Eval(s) If s <> "" Then MsgBox s, vbInformation End If End Sub
private void menuItemEvalJavaScript_Click(object sender, EventArgs e) { string input = Microsoft.VisualBasic.Interaction.InputBox("JavaScript", "HTML5Pad", "", 500, 300); if (input != "") { WebKitXCEF31.Eval(input); if (input != "") { MessageBox.Show(input, "HTML5 Pad", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }