动态加载js或ajax跨域通用方法

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值