1 1 集成了另外一个web程序,我们把那个web程序放在我们提供的iframe里。那个web程序只有在第一次初始化的时候是一个普通的HTTP请求,之后在页面上的操作,都是通过ajax来操作的。现在我们有个需求,我们需要知道iframe里面做了什么操作。
没法改别人的代码,那我们只能监测里面的事件了,所以总归来说,要找到一种方法来检测到iframe里面的所有ajax请求。
下面是我的请求 发现无法执行 ajaxsend
求改进下 能监听到 子页面得ajax 请求
<iframe id='mainIframe' name='mainIframe' src="https://mobile.peilian.com/promotion_f2?utm_source=szjs&utm_medium=cpa&utm_campaign=time0306&utm_term=&utm_content=" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" width="1500" height="900" allowfullscreen="true" ></iframe>
<!--onload="ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);"-->
<div></div>
<script>
iframe_add_loadevent();
function iframe_add_loadevent() {
var my_frame =document.getElementById('mainIframe');
alert(1);
if(my_frame){
alert(2);
if(my_frame.attachEvent){ //ie
alert(3);
my_frame.onreadystatechange=function () {
if (this.readState == 'complete') {
ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);
}
};
} else{ //other
alert(4);
my_frame.onload = function () {
alert(5);
ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);
};
}
}
// if(my_frame){
// my_frame.onload = function () {
// ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);
// }
// }
}
function ajaxSend(objectOfXMLHttpRequest, callback) {
alert(10);
// http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event
if(!callback){
return;
}
var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = objectOfXMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = objectOfXMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
// this.method :the ajax method used
// this.url :the url of the requested script (including query string, if any) (urlencoded)
// this.data :the data sent, if any ex: foo=bar&a=b (urlencoded)
alert(2);
alert(this.method);
callback(this.method, this.url, this.data);
}
objectOfXMLHttpRequest.prototype.open = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempOpen.apply(this, arguments);
s_ajaxListener.method = a;
s_ajaxListener.url = b;
if (a.toLowerCase() == 'get') {
s_ajaxListener.data = b.split('?');
s_ajaxListener.data = s_ajaxListener.data[1];
}
}
objectOfXMLHttpRequest.prototype.send = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempSend.apply(this, arguments);
if(s_ajaxListener.method.toLowerCase() == 'post') {
s_ajaxListener.data = a;
}
s_ajaxListener.callback();
}
}
function onAjaxSend(method, url, data) {
alert('ajax');
console.log("method"+method);
console.log(url);
console.log(data);
}
</script>