Backgound
有两个业务系统,业务人员每天都要进行大量操作。这两个系统不开放接口,唯有通过Geek的方法。
Tools
trixie, IE8下无效。
ie7pro, 附带的东西太多,需要配置,较麻烦。
crossider, 支持IE7+,傻瓜式安装。
最后选用crossider。
Functions
1. Get iframe document
IE的跟节点是contentWindow,而其他是contentDocument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| function get_frame_document(frame_id){
var doc;
var iframeObject = document.getElementById(frame_id);
if(iframeObject == null){
return undefined;
}
if (iframeObject.contentDocument) { // DOM
doc = iframeObject.contentDocument;
}
else if (iframeObject.contentWindow) { // IE win
doc = iframeObject.contentWindow.document;
}
return doc;
}
|
2. Overwrite showModalDialog function
showModalDialog 打开的window无法访问其DOM,故重写该方法,使用window.open打开。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| var myIframeWin = get_frame_window('frameMain')
myIframeWin._showModalDialog = myIframeWin.showModalDialog;
myIframeWin.showModalDialog = function(url,dialogArguments,sFeatures){
var regexp = new RegExp('my_pattern')
if(!regexp.test(url)){
return myIframeWin._showModalDialog(url,dialogArguments,sFeatures)
}
var w = myIframeWin.open(url, '_blank');
w.dialogArguments = dialogArguments;
w._close = w.close;
w.close = function(){
// do something;
w._close();
}
}
|
3. Child window pass variable to parent window
3.1 window.opener not recieve object
1
2
| my_object = {}
window.opener.my_var = my_object;
|
parent window的my_var为undefined.
Solution:将JSON转换成String
1
| window.opener.my_var = JSON.stringify(window.returnValue);
|
3.1 IE7 不支持 JSON.stringify
Solution: include JSON3 lib.
http://bestiejs.github.io/json3/
4. ajax sync not work.
Solution: load script by script tag.
1
2
3
4
| function ajax_through_script(url, params){
var dom = $('<script />').attr('src', url + '?'+params);
dom.appendTo($('body'));
}
|