关于脚本回调
关于callback,在某年拜读emu的blog时发现了一种和谐的方案
主要解决了回调函数名可配置的问题.
并且还实现了强大的错误回调
链接如下:
http://www.blogjava.net/emu/articles/129240.html
主要实现方案是
ie用DocumentFragment 或 htmlfile
其它浏览器用iframe
不过当初测试iframe用伪协议有点兼容问题.
后来就换成write了.
如今在测试发现伪协议src又兼容了= =
不过又发现khtml的浏览器不会带referer
最终测试
frame src为伪协议 都不会带referer
如果frame src先为随意页面a
在load时在write或改src为伪协议
sf, chrome 会带 a的url作referer
op, ff 会带 调用页的url 作referer
最终代码如下
var callbackJS = function () {
var ua = navigator.userAgent;
if (/MSIE/.test(ua)) {
return function (param) {
charset = param.charset || 'gb2312'
var frag = document.createDocumentFragment(), script = frag.createElement('script');
script.charset = charset;
frag[param.name] = function () {
param.callback && param.callback.apply(null, arguments);
frag = script = script.onreadystatechange = frag[param.name] = null;
};
script.onreadystatechange = function () {
if (script.readyState == 'loaded') {
param.errorcallback && param.errorcallback();
frag = script = script.onreadystatechange = frag[param.name] = null;
}
};
script.src = param.url;
frag.appendChild(script);
};
} else {
return function (param) {
charset = param.charset || 'gb2312'
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.callback = function () {
param.callback && param.callback.apply(null, arguments);
iframe.callback = iframe.errorcallback = null;
iframe.src = 'about:blank', iframe.parentNode.removeChild(iframe), iframe = null;
};
iframe.errorcallback = function () {
param.errorcallback && param.errorcallback();
iframe.callback = iframe.errorcallback = null;
iframe.src = 'about:blank', iframe.parentNode.removeChild(iframe), iframe = null;
};
try {
if (/KHTML/.test(ua)) iframe.src = 'about:blank';
iframe.onload = function () {
iframe.onload = null;
//iframe.src="javascript:\"<scrip"+"t>function " + param.name + "(){frameElement.callback.apply(null, arguments)};<\/scrip"+"t><scrip"+"t src='"+param.url+"'><\/scrip"+"t><scrip"+"t>setTimeout('frameElement.errorcallback()',0)<\/scrip"+"t>\"";
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(
'<script type="text\/javascript">function ' + param.name + '() { frameElement.callback.apply(null, arguments); }<\/script>'
+ '<script type="text\/javascript" src="' + param.url + '" charset="' + charset + '"><\/script>'
+ '<script type="text\/javascript">window.setTimeout("try { frameElement.errorcallback(); } catch (exp) {}", 1)<\/script>'
);
iframe.contentWindow.document.close();
};
document.body.insertBefore(iframe, document.body.firstChild);
} catch (exp) {}
};
}
}();
var ua = navigator.userAgent;
if (/MSIE/.test(ua)) {
return function (param) {
charset = param.charset || 'gb2312'
var frag = document.createDocumentFragment(), script = frag.createElement('script');
script.charset = charset;
frag[param.name] = function () {
param.callback && param.callback.apply(null, arguments);
frag = script = script.onreadystatechange = frag[param.name] = null;
};
script.onreadystatechange = function () {
if (script.readyState == 'loaded') {
param.errorcallback && param.errorcallback();
frag = script = script.onreadystatechange = frag[param.name] = null;
}
};
script.src = param.url;
frag.appendChild(script);
};
} else {
return function (param) {
charset = param.charset || 'gb2312'
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.callback = function () {
param.callback && param.callback.apply(null, arguments);
iframe.callback = iframe.errorcallback = null;
iframe.src = 'about:blank', iframe.parentNode.removeChild(iframe), iframe = null;
};
iframe.errorcallback = function () {
param.errorcallback && param.errorcallback();
iframe.callback = iframe.errorcallback = null;
iframe.src = 'about:blank', iframe.parentNode.removeChild(iframe), iframe = null;
};
try {
if (/KHTML/.test(ua)) iframe.src = 'about:blank';
iframe.onload = function () {
iframe.onload = null;
//iframe.src="javascript:\"<scrip"+"t>function " + param.name + "(){frameElement.callback.apply(null, arguments)};<\/scrip"+"t><scrip"+"t src='"+param.url+"'><\/scrip"+"t><scrip"+"t>setTimeout('frameElement.errorcallback()',0)<\/scrip"+"t>\"";
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(
'<script type="text\/javascript">function ' + param.name + '() { frameElement.callback.apply(null, arguments); }<\/script>'
+ '<script type="text\/javascript" src="' + param.url + '" charset="' + charset + '"><\/script>'
+ '<script type="text\/javascript">window.setTimeout("try { frameElement.errorcallback(); } catch (exp) {}", 1)<\/script>'
);
iframe.contentWindow.document.close();
};
document.body.insertBefore(iframe, document.body.firstChild);
} catch (exp) {}
};
}
}();