Team, I want the browser to read a property file from server. So i am following Jquery/AJaX like below.
<script>
var properties = null;
$(document).ready(function(){
$.ajax({url:"demo_test.txt",success:function(result){
properties = result;
//properties = $('#result').val()
//jQuery.globalEval("var newVar = result;")
document.write("inside " + properties);
}});
});
document.write("outside " + properties );
</script>
Here "inside " is properly printing the file chars. But " outside " is printing null for properties.
- Why?
- I am able to see "outside " output on the page; only if i comment "inside " line? Why so?
- How to get the jquery result for further processing on data?
- Can i have a property file (key=value) in the server and is there any facility offered by jquery/ajax similar to java.util.property :: getValue("key")? if the above is not possible; can i keep the property file as JSON or XML file and any utilities offered by Ajax to get value for a key??
Update:
I have made some research and updating the answer for Q4.
I am not sure how to read properties file, but i gave solution for xml/json file reading.
After changing Synchronous Ajax call like below
var properties = null;
$.ajax({
url : "demo_test.txt",
async : false,
success : function(result)
{
properties = result;
document.write("inside " + properties);
}
});
If the server side has a XML file below is the way to parse:
<?xml version="1.0"?>
<server>
<name>velu</name>
</server>
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(property,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(property);
}
alert(xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue);
If you have json content on the server side then
{
"name":velu
}
var obj = JSON.parse(property);
alert(obj.name);
Javascript style for accessing the file (Asynchronus)
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{// listener
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
property = xmlhttp.responseText;
}
}
xmlhttp.open("GET","demo_test.txt",true);
xmlhttp.send();
while (property != null) {
alert(property);
break;
}