我有几个在我的索引页面中加载的JS脚本,包括一个包含
的JS脚本function ajaxrequest(php_file, purpose, var1, var2, var3, var4, var5, var6, var7, var8, where) {
var request = getXMLHTTP(); // call the function for the XMLHttpRequest instance
// create pairs index=value with data that must be sent to server
var the_data = 'purpose=' + purpose + '&var1=' + var1 + '&var2=' + var2 + '&var3=' + var3 + '&var4=' + var4 + '&var5=' + var5 + '&var6=' + var6 + '&var7=' + var7 + '&var8=' + var8;
request.open("POST", php_file, true); // set the request
// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(the_data); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(where).innerHTML = request.responseText;
}
};
}
在调用此函数之前,所有内容(包括与上述函数在同一文件中找到的其他JS函数)都能正常工作。这个函数每次都有效,但任何JS函数,PHP都需要,session_start()似乎都在重置'并需要重新加载。这是正常的,如果没有,请你协助纠正这个问题吗?
答案 0 :(得分:0)
如果您希望该代码影响AJAX完成打印后创建的任何新元素,则需要在加载AJAX页面后调用依赖页面项的函数。
当我调用一个母函数时,我将我在JS中运行的所有函数包装起来,有点像这样:
// In the beginning
function init(){
// Functions that need to be fired when any page loads
// etc etc
// Code goes here...
}
function ajaxrequest(php_file, purpose, var1, var2, var3, var4, var5, var6, var7, var8, where) {
// ...
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(where).innerHTML = request.responseText;
init();
}
};
}