ajax需要引用什么js文件吗,如何在ajax调用中包含js文件?
嘿,我找到了添加它的方法.... :)
注意 - 這是一個同步過程,所以你不必擔心腳本是否加載....腳本將始終加載實例你調(diào)用該函數(shù),你可以立即開始使用加載的腳本..
讓我們使用這兩個功能
1)第一個是用于檢索值的ajax函數(shù)
async應為true以同步發(fā)送請求
// AJAX FUNCTION
function loadXMLDoc(reqt,url,reqp,cfunc,async)
{
var xmlhttp;
try// code for IE7+, Firefox, Chrome, Opera, Safari
{
xmlhttp=new XMLHttpRequest();
}
catch(err)// code for IE6, IE5
{
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E){}
}
}
if(!xmlhttp)
{
alert("error");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
cfunc(xmlhttp.responseText);
}
}
if(reqt=='GET')
{
url+=(reqp!=""?"?":"")+reqp;
xmlhttp.open("GET",url,(async?false:true));
xmlhttp.send();
}
else if(reqt=='POST')
{
xmlhttp.open("POST",url,(async?false:true));
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(reqp);
}
else
{
return false;
}
}
/*use this function
loadXMLDoc(reqt,url,reqp,function(response){
});
*/
2)然后我們使用ajax將js文件作為字符串加載,然后將其附加到新腳本標簽的innerHTML然后將其附加到head部分,還有一件事要確保文件已經(jīng)加載我使用了腳本標記的id作為文件的路徑,這使得檢查重復的任務真的很容易...... :)
//add new script dynamically
function add_script(src)
{
if(!document.getElementById(src))
{
loadXMLDoc("GET",src,"",function(jsresp){
var head = document.getElementsByTagName("head")[0];
var script=document.createElement("script");
script.type='text/javascript';
script.id=src;
script.text=jsresp;
head.appendChild(script);
},true);
}
}
感謝我以前獲得的所有幫助,并將從本網(wǎng)站及其用戶獲取用于開發(fā)目的...
關于VIPIN JAIN
總結
以上是生活随笔為你收集整理的ajax需要引用什么js文件吗,如何在ajax调用中包含js文件?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10怎么关闭睡眠密码 关闭Win1
- 下一篇: 三菱plcfx5u指令手册_从西门子20