Demo
JSON result
ajax.js
var ajax = {
remove :function(elm){if(elm){if(elm.parentNode){elm.parentNode.removeChild(elm);}}},
include :function(srcFile,callback)
{
if(!ajax.isLoad){ajax.isLoad={};}
var i,j,h,
s=srcFile,
f=callback,
t=(ajax.isLoad[s])?true:false,
doc=document;
if(t){ajax.remove(ajax.isLoad[s]);}
i=doc.createElement('script');
i.setAttribute('type','text/javascript');
i.setAttribute('src',s);
h=doc.getElementsByTagName('body')[0];
if(typeof f=="function")
{
if(/*@cc_on!@*/false)
{
i.onreadystatechange=function()
{
j=this.readyState;
if(j=="loaded"||j=="complete")
{
f(i);
}
};
}
else
{
i.onload=f;
}
}
h.appendChild(i);
ajax.isLoad[s]=i;
}
}
How to used
ajax.include("your_dynamic_url_here", your_callback_function);
This demo
function callback()
{
if(typeof myOtherFunction == "function")
{
myOtherFunction();
}
if(typeof JSON == "object")
{
document.getElementById("demo").innerHTML = JSON["result"];
}
}
function call1()
{
ajax.include("mon_site_distant.js",callback);
}
function call2()
{
ajax.include("mon_site_distant_2.js?param=2",callback);
}
mon_site_distant.js
var JSON = {"result":"my json result"};
function myOtherFunction()
{
alert("myOtherFunction");
}
mon_site_distant_2.js
var JSON = {"result":"my json result 2"};
function myOtherFunction()
{
alert("myOtherFunction 2");
}