I'm using
getAppletContext().showDocument(new URL("javascript:" + command));
to call javascript from applet.
But sometimes in firefox this doen't work, I don't know why.
So, now I'm trying to use JSObject, using this:
JSObject jsObject = new JSObject();
jsObject.eval(command);
But I got this error:
Exception in thread "thread applet-com.foo.bar.TestApplet-6"
java.lang.InstantiationError: netscape.javascript.JSObject
Why I'm getting this error?
There is another way to do java applet to javascript communication?
I'm using
JSObject jsObject = JSObject.getWindow(this);
jsObject.eval(....);
And it works now.
Related
I am new to iOS and I am not getting how to call an objectiveC method from Javascript using web view.
I have already tried this but it didn't work. Additionally, please help me with my objectiveC code.
function IosSuccess(transactionid)
{
alert("Transaction successful,transaction id :" +transactionid);
var ret = jsb.reflection.callStaticMethod("PaymentViewController","TransactionId:",""+transactionid);
}
You cannot call objective c method using JavaScript directly rather you have to use JavaScriptCore.framework and making an interface for call back will do the required task.
I want to switch from an embedded IE activeX to the libcef framework. My web project's javascript call C++ function use window.external.xxx method. But I can't get window.external object in cef framework. I try to bind my c++ function in window object. sadly, it doesn't work for me.
My code for binding c++ function to window object is like that:
CefRefPtr<CefV8Value> ptrGlobalObj = context->GetGlobal();
CefRefPtr<CefV8Value> jsCallOrthoLink = CefV8Value::CreateFunction(_T("CallOrthoLink"), m_ptrV8Handler);
ptrGlobalObj->SetValue(_T("CallOrthoLink"), jsCallOrthoLink, V8_PROPERTY_ATTRIBUTE_NONE);
I test it with window.xxx method in javascript. it works. so I know my bind codes are correctly.
How can I fixed this issue with window.external.xxxx method?
Try this:
external = CefV8Value::CreateObject(NULL, NULL)
external->SetValue("CallOrthoLink", jsCallOrthoLink, V8_PROPERTY_ATTRIBUTE_NONE)
global->SetValue("external", external, V8_PROPERTY_ATTRIBUTE_NONE)
Calling Javascript functions running inside Rhino from Java is easy enough - that after all is why Rhino was created. The thing I am having trouble establishing is this:
Context: I have a Phonegap CLI (v 6.3.3) Android project (API 19+) where I do a great deal of processing via loadable JavaScript running inside rhino
A Phonegap plugin - which I am creating at the same time as the actual Phonegap app - contains class called Storage which provides public, static, methods such as readFromFile(String fileName), writeToFile(String fileName,String data) etc.
What I want to be able to do is to call Storage.readFromFile etc from my loaded JavaScript code in Rhino.
Just how this should be done is not too clear to me. From the searches I have done thus far it involves using ScriptableObject.putProperty to pass the Java class in question, Storage in my case to JavaScript. However, how this should be done and then how it should be used at the JS end leaves me rather confused.
I would be most grateful to anyone here who might be able to point me in the right direction
Given that Rhino has less than 100 followers here it should perhaps come as little surprise that this question was not answered. In the mean time I have managed to find the solution myself and it turns out to be very simple. I share it below for the benefit of anyone else running into this thread.
My Storage class is very simple. It goes something like this
public class Storage
{
public static boolean haveFile(){}
public static boolean readFromFile(String fname){}
...
}
When I call Javascript from Java via Rhino I simply pass a new instance of the Storage class as the last of my function parameters
Context rhino = Context.enter();
Object[] functionParams = new Object[] {"Other parameters",new Storage()};
rhino.setOptimizationLevel(-1);
try
{
Scriptable scope = rhino.initStandardObjects();
String rhinoLog = "var log = Packages.io.vec.ScriptAPI.log;";
String code = /*Javascript code here* as shown separately below/;
rhino.evaluateString(scope, rhinoLog + code, "ScriptAPI", 1, null);
Function function = (Function) scope.get("jsFunction", scope);
Object jsResult = function.call(rhino,scope,scope,functionParams);
}
where the Javascript code is
function jsFunction(a,s)
{
//a - or a,b,c etc - here will be the "other" parameters
//s - will be the instance of the Java side Storage class passed above
//now you can do things like
s.writeToFile('fileName','fileData');
var fd = s.readFromFile('fileName');
s.dropFile('fileName');
...
}
I have registered a .NET DLL using REGASM and registration is successful. I'm able to create object of the classes and use them in my Javascript. The class I consume i sa non static one.
This is how I'm using and it is working,
var objDP = new ActiveXObject("PE.Core.PacketExtractor");
objDP.PrintMessage();
objDP.Start();
My problem :
I have a methods in my C# class which I would like to call and get the value. The method signature is given below,
Signature : Get_Current_Faults(ref PEFaults my_faults);
Usage : Get_Current_Faults(my_faults);
In this method the "PEFaults" is a public C# Struct. This is also COM Visible and registered as I could see an entry in the Registry.
I tried to get data in JavaScript by doing the following,
var objFault = new ActiveXObject("PE.Core.PE_FAULTS");
objDP.Get_Current_Faults(ref objFault);
WScript.echo(objFault.Temperature);
But this is not working and giving an error "Automation Server cant create an object" # PE.Core.CURRENT_FAULTS object creation.
Cant we create an object of Struct in JS ? Any idea how to handle a ref parameter ? How else I can make this work ?
Please advise me. Thanks.
Can any one please help me how to call dll functions from javascript. while using activexobject I am getting error "automation server cannot create object". Here is my code
var jMyAcctId = document.all.RefNum.value;
var jMyAcctType = document.all.TrxType.value;
var NewObject = new ActiveXObject("HDMFCDV.cdv");
if (NewObject.IsValidID(jMyAcctId,jMyAcctType) == true)
{
document.all.RefNumError.innerText = "";
CnvUp(sel);
document.all.CustFName.disabled = false;
document.all.CustFName.focus();
}
Thanak in advance.
Your JavaScript code is good. I suspect the problem is with the HDMFCDV.cdv ActiveX - either they way you implemented it or they way you registered it.
I'm not familiar with HDMFCDV object. Is that a proprietary object you implemented? Here are few tips to troubleshoot:
Make sure your object is registered (did you run regsrv32?)
Verify HDMFCDF.cdv is in the registry: HKCR\HDMFCDF.cdv
Make sure there is a CLSID
Make sure the class ID is in the registry, and that it points to the DLL implementing your object. HKCR\CLSID{your-guide}\InprocServer32 (REG_SZ)
A very common lookout: have you implemented IObjectSafety. Without this interface, and without this interface returning that it is safe for untrusted caller, IE will refuse to instantiate this object