22 / 11 / 20
[ComVisible(true)]
class cc {
public int and(int a, int b) {
return a + b;
}
}
cc obj = new cc();
webBrowser1.ObjectForScripting = obj;//2、设置js中window.external对象代表的类
js中调用
let c = window.external.and(1, 2);
由于webbrowser控件使用的是 ie 较低版本的内核,在引入外部js中存在 console.log 时会异常,处理办法:
方法1: 参考文章:https://segmentfault.com/a/1190000021972697?sort=newest
(function(g) {
'use strict';
var _console = g.console || {};
var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'exception', 'error', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
var console = {version: '0.2.0'};
var key;
for(var i = 0, len = methods.length; i < len; i++) {
key = methods[i];
console[key] = function (key) {
return function () {
if (typeof _console[key] === 'undefined') {
return 0;
}
// 添加容错处理
try {
Function.prototype.apply.call(_console[key], _console, arguments);
} catch (exp) {
}
};
}(key);
}
g.console = console;
}(window));
方法1:
if (!window.JSON) {
window.JSON = {
parse: function (jsonStr) {
return eval('(' + jsonStr + ')');
},
stringify: function (jsonObj) {
var result = '',
curVal;
if (jsonObj === null) {
return String(jsonObj);
}
switch (typeof jsonObj) {
case 'number':
case 'boolean':
return String(jsonObj);
case 'string':
return '"' + jsonObj + '"';
case 'undefined':
case 'function':
return undefined;
}
switch (Object.prototype.toString.call(jsonObj)) {
case '[object Array]':
result += '[';
for (var i = 0, len = jsonObj.length; i < len; i++) {
curVal = JSON.stringify(jsonObj[i]);
result += (curVal === undefined ? null : curVal) + ",";
}
if (result !== '[') {
result = result.slice(0, -1);
}
result += ']';
return result;
case '[object Date]':
return '"' + (jsonObj.toJSON ? jsonObj.toJSON() : jsonObj.toString()) + '"';
case '[object RegExp]':
return "{}";
case '[object Object]':
result += '{';
for (i in jsonObj) {
if (jsonObj.hasOwnProperty(i)) {
curVal = JSON.stringify(jsonObj[i]);
if (curVal !== undefined) {
result += '"' + i + '":' + curVal + ',';
}
}
}
if (result !== '{') {
result = result.slice(0, -1);
}
result += '}';
return result;
case '[object String]':
return '"' + jsonObj.toString() + '"';
case '[object Number]':
case '[object Boolean]':
return jsonObj.toString();
}
}
};
}
方法2: 参考文章:链接1
// html顶部添加
<!DOCTYPE>
// head中添加
<meta http-equiv="X-UA-Compatible" content="IE=8" >