src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/html/javascript/xmlhttp_data.js

187 lines
5.0 KiB
JavaScript

/**
* Execute javascripts and parse html
*
*/
function sethtml(content) {
var search = content;
var loc = 0;
var script;
var endscript;
while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i))
{
search = search.substr(search.indexOf(script[0]) + script[0].length);
if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;
block = search.substr(0, search.indexOf(endscript[0]));
search = search.substring(endscript[0].length + endscript[0].length);
var oScript = document.createElement('script');
oScript.text = block;
if (oScript.innerHTML.search('location.href') != -1) {
loc = 1;
}
document.getElementsByTagName("head").item(0).appendChild(oScript);
}
return loc;
}
/**
* Retrieve xmlhttp data
*
*/
function xmlhttp_data(href, div, async, base, id, post_param, get_param, func) {
// Initial values
var result = '';
var xmlrequest = null;
var loc = 0;
// Asynchronous/synchronous
var async_action = (((typeof(async) != 'undefined') && (async != '')) || (async == 1)) ? 1 : 0
try {
xmlrequest = new XMLHttpRequest();
}
catch (e) {
try {
xmlrequest = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
xmlrequest = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
xmlrequest = null;
}
}
}
if (!xmlrequest && window.createRequest) {
try {
xmlrequest = window.createRequest();
}
catch (e) {
xmlrequest = null;
}
}
// Return empty string when no xmlhttp request could be created
if (xmlrequest != null) {
if (async_action) {
xmlrequest.onreadystatechange = function() {
// Return value?
if (xmlrequest.readyState == 4) {
if (xmlrequest.status == 200) {
// Skip when no response empty
if (xmlrequest.responseText.length > 0) {
// Handle all javascripts => redirect?
loc = sethtml(xmlrequest.responseText);
// Set innerHTML
if ((typeof(div) != 'undefined') && (div.length) && (div != '')) {
if (!loc) {
try {
getElement(div).innerHTML = xmlrequest.responseText;
}
catch (err) {}
}
}
else {
result = xmlrequest.responseText;
}
}
}
// Assign an empty function to the state handler;
xmlrequest.onreadystatechange = function () {}
// Abort current transfer. This will also call the state handler, which is not what we want
xmlrequest.abort();
// Clear object
xmlrequest = null;
if ((!loc) && (typeof(func) != 'undefined')) {
// function pointer
try {
func(result);
}
catch(err) {}
}
}
}
}
// action => synchronous/asynchronous
var action = (async_action) ? true : false;
// Post parameters?
params = '';
if ((typeof(post_param) == 'string') && (post_param.length) && (post_param != '')) {
params += post_param;
}
// href + get parameters?
href_get_param = base + href + '?id=' + id;
if ((typeof(get_param) == 'string') && (get_param.length) && (get_param != '')) {
href_get_param += get_param;
}
xmlrequest.open('POST', href_get_param, action);
//Send the proper header information along with the request
xmlrequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlrequest.send(params);
if (!async_action) {
// Return value?
if (xmlrequest.status == 200) {
// Skip when no response empty
if (xmlrequest.responseText.length > 0) {
// Handle all javascripts => redirect?
loc = sethtml(xmlrequest.responseText);
// Set innerHTML
if ((typeof(div) != 'undefined') && (div.length) && (div != '')) {
if (!loc) {
getElement(div).innerHTML = xmlrequest.responseText;
}
}
else {
result = xmlrequest.responseText;
}
}
}
// Assign an empty function to the state handler;
xmlrequest.onreadystatechange = function () {}
// Abort current transfer. This will also call the state handler, which is not what we want
xmlrequest.abort();
// Clear object
xmlrequest = null;
if ((!loc) && (typeof(func) != 'undefined')) {
// function pointer
try {
func(result);
}
catch(err) {
console.log(err);
}
}
}
}
else {
// Asynchronous request; return the Ajax object so the caller can
// process it further, where necessary
result = xmlrequest;
}
return result;
}