function loadJSByFrame(url, success) {
var iframe = document.createElement('iframe');
iframe.width = '0';
iframe.height = '0';
iframe.scrolling='no';
iframe.frameborder='0';
iframe.style.display = 'none';
success = success || function() {};
iframe.src = url;
if(iframe.attachEvent){
iframe.attachEvent("onload",function(){
success();
});
}else{
iframe.onload = function() {
success();
}
}
document.body.appendChild(iframe);
}
对应的jquery方法:
$.getScript("http://a.js?b=c", function() { alert('ok');});
$.getJSON("http://a.js?b=c&callback=?", function(data){ alert('ok');});
$.ajax({
url: 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',
dataType: "script",
success: function(){
alert(remote_ip_info.country);
//处理data数据
}
});
此外,如果只是访问某url(并不执行加载的内容),并在加载完成后执行指定的函数,可以
方法1.
function loadJSByFrame(url, success) {
var iframe = document.createElement('iframe');
iframe.width = '0';
iframe.height = '0';
iframe.scrolling='no';
iframe.frameborder='0';
iframe.style.display = 'none';
success = success || function() {};
iframe.src = url;
if(iframe.attachEvent){
iframe.attachEvent("onload",function(){
success();
});
}else{
iframe.onload = function() {
success();
}
}
document.body.appendChild(iframe);
}
方法2.
function loadJSByImg(url) {
var img = document.createElement('img');
img.width = '0';
img.height = '0';
img.style.display = 'none';
// document.body.appendChild(img);
img.src = url;
}
http://blog.csdn.net/programflag/article/details/6852196