
Create an XML HTTP Request (XHR) object
function create_XMLHttpRequest() { //create new XHR object if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; }
Send request to Server and receive response from server
- Get replay from php web server: serverResponseInfo=httpGetRequest("http://domain/myReply.php")
- Read content from a text file: serverResponseInfo=httpGetRequest("myTextFile.txt")
- Read content from a text file: document.getElementById("myDiv").innerHTML=loadXMLDoc("myTextFile.txt");
function httpGetRequest(url)
{ xmlhttp= create_XMLHttpRequest(); xmlhttp.open("GET",url,false); xmlhttp.send(); return xmlhttp.responseText; } |
Check if file exist
- To check a file if exist in www root directory: fileExists("/settings.xml")
- To check a file if exist in the current directory: fileExists("settings.xml")
function fileExists(filename) { xmlhttp= create_XMLHttpRequest(); //create new XHR object if(!xmlhttp) return false; try { xmlhttp.open("HEAD", filename, false); xmlhttp.send(null); return (xmlhttp.status==200) ? true : false; } catch(er) { return false; } }
Load an XML file using XML HTTP Request
Eg. loadXML("settings.xml")
function loadXML(XML_filename) { xmlhttp= create_XMLHttpRequest(); xmlhttp.open("GET",XML_filename ,false); xmlhttp.send(); return xmlhttp.responseXML; }
Convert XML to string
function xmlToString(xmlDoc){ if(xmlDoc.xml){ // MSIE xmlString = xmlDoc.xml; }else{ xmlString = (new XMLSerializer).serializeToString(xmlDoc); } return xmlString; }
Create a string based on date
Example of output: 2014_0502
function createString_accordingDate() { var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1;//January is 0! var yyyy = today.getFullYear(); if(dd<10){dd='0'+dd} ; if(mm<10){mm='0'+mm}; return (yyyy + "_" + mm + dd); }
.