How does one access the methods of an embedded ActiveX control using jQuery?
I have embedded in my page an ActiveX control which communicates with a printer and an LED display. Is there a way to access, using jQuery, the methods available within the AX control? For example:
$("#plugin").updateDisplay("COM6:", "test");
$("#plugin").printReceipt("COM5:", "\x0a\x0a\x1dV\x42\x00");
$("#plugin").openDrawer();
I know the above doesn't work开发者_如何学JAVA, but is there some similar way of doing the equivalent using jQuery?
Also, the embedded code looks as such:
<object id="plugin" type="application/x-ticket" width="1" height="1">
<param name="onload" value="pluginLoaded" /></object>
I can access the methods using JavaScript outside of jQuery, but I thought perhaps there was a way to access the methods using jQuery.
What benefit do you get out of using jquery? It's perfectly fine to use straight JS with jQuery. document.getElementById('plugin').updateDisplay('blah', 'bleh')
. But if you really want to, you can create a plugin.
jquery.fn.updateDisplay = function(a, b) {
this.each(function(index, el){
el.updateDisplay(a, b);
});
return this;
}
//... and so on
Here's a generic way of doing it:
function convertMethodsTojQuery(/* methodName1, methodName2, ...*/) {
// In some browsers (*cough* IE *cough*), the methods of DOM
// objects are not real JavaScript Functions and don't
// support the apply method. Borrow Function's apply.
var apply = Function.prototype.apply;
for (var i=0; i < arguments.length; i++) {
var methodName = arguments[i];
jQuery.fn[methodName] = (function(name) {
return function() {
// Save arguments so it's available in the inner looping closure
var args = arguments;
this.each(function(index, el){
apply.call(el[name], el, args);
});
}
})(name);
}
}
// Call it like
convertMethodsTojQuery("updateDisplay", "printReceipt", "openDrawer")
The generic method is pretty involved (borrowing Function.prototype and self calling functions to isolate the looping variable), so it's not easy to understand unless you have a good grasp of JavaScript
精彩评论