用JavaScript实现jQuery的$.getJSON和$load()的方法
最简单的方法 动态加载 js
var s = document.createElement('script'); s.src = "http://xxxx.com/api.php?callback=callback"; document.body.append(s);
完整方法:
$ = { getjs on:function(url,data,callback){ var call = this.urlquerystring(url)["callback"]; for(var key in data){ url += "&"+key+"="+data[key]; } //window[call] = callback; window[call] = function(data){ window[call] = undefined; try{ delete window[call]; }catch(e){} callback(data); } var script = document.createelement("script"); script.type = "text/javascript"; script.src = url; document.getelementsbytagname("body")[0].appendchild(script); script.onload = function(){ document.getelementsbytagname("body")[0].removechild(this); } }, urlquerystring:function(url){//api.php?callback=callback&foo=foo var url = url.split("?")[1]; var result = {},reg = /([^&=]+)=([^&]*)/g,m; while(m = reg.exec(url)){ result[m[1]] = m[2]; } return result;//object {callback: "callback", foo: "foo"} } }; $.getjs on("api.php?callback=callback&foo=foo",{"aa":11,"bb":22},function(data){ console.log(data); });
...jQuery源码里全有,打开源码,找到方法名一搜索就有了......................................
load ajax js网上百度一下很多