覆盖外部对象's函数在Internet Explorer中使用JavaScript
Overriding external object's function using JavaScript in Internet Explorer
我目前正在处理一个仅IE的项目,该项目利用了主机应用程序提供的外部对象模型。Internet Explorer允许通过浏览器组件访问此外部对象:http://msdn.microsoft.com/en-us/library/ie/ms535246(v=vs.85).aspx
对对象的访问采用JavaScript函数调用的形式,类似于:
external.MethodName(arg1, arg2);
应用程序流最近的一个变化将在这些JavaScript调用周围引入数百甚至数千个if语句条件,例如:
if (X) {
external.MethodName(arg1, arg2);
} else {
// do something else
}
与其修改潜在的数千个HTML文件,不如重写外部对象的函数,使if条件只出现在一个地方。通常,这可以在JavaScript中使用来完成
external.OldMethodName = external.MethodName;
external.MethodName = function(arg1, arg2) {
if (X) {
external.OldMethodName(arg1, arg2);
} else {
// do something else
}
};
但是,这会导致"无效的过程调用或参数"脚本错误,因为不能以这种方式引用外部宿主方法。
我没有访问主机应用程序的专有代码来直接更改外部方法。
有没有什么方法可以使用JavaScript覆盖外部对象的函数,或者我需要用if语句包装(潜在的)数千次调用(这是一种非常糟糕的做法)?
更新:在与客户反复讨论之后,我们设法联系了第三方供应商来更新外部主机方法,这比我们在前端包装方法要好得多。在此期间,我已经接受了保罗的回答。
使用toString()和eval:
var foo = external.MethodName.toString().replace("OldMethodName", "MethodName").replace("bar","baz");
eval(foo);
if(x)
{
external.OldMethodName(arg1,arg2);
}
else
{
MethodName(arg1,arg2)
}
相关文章:
- 视频HTML没有'无法在Internet Explorer 11上工作
- Internet Explorer缺少占位符支持,特别是密码字段
- Internet Explorer中的文本换行错误
- 为什么我在Internet Explorer上看不到html元素
- internet explorer 8兼容模式下的Youtube api错误
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- Internet Explorer 8. Events
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- element.dataset in Internet Explorer
- Facebook FB.init”;访问被拒绝”;在Internet Explorer 11中
- JavaScript不适用于internet explorer和Firefox,但适用于谷歌
- 在Internet Explorer中,向所选内容添加选项不起作用
- 在Internet Explorer中从二进制文件打开PDF
- elementproblem internet explorer
- 我可以使用哪些方法在Internet Explorer中禁用Alt+*X*
- 如何在Internet Explorer中使用javascript设置cookie
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- sharepoint站点的internet explorer中出现Javascript错误
- Internet Explorer重新加载IFrame
- Internet Explorer Internet Failure HTTP Status Codes