mayurpuli Posted November 5, 2008 Share Posted November 5, 2008 Hi All, I am newbie to this and i am learning Ajax. I am running a the below script in a html file and i get the error mentioned in the subject line <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Ajax Demo</title> <script language="javascript" > var XMLHttpRequestObject = false; if(window.XMLHTTPRequestObject) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } function getdata(dataSource, divID) { if(XMLHttpRequestObject) { var obj = document.getElementById(divID); XMLHttpRequestObject.open("GET", dataSource); XMLHttpRequestObject.onreadystateChange = function() { if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { obj.InnerHTML = XMLHttpRequestObject.responseText; } } XMLHttpRequestObject.send(null); } } </script> </head> <body> <h1> An Ajax Demo </h1> <form> <input type = "button" value = "Fetch Message" onclick="getdata('./data.txt', 'targetDiv')"> </form> <div id="targetDiv"> <p> The fetched message will appear </p> </div> </body> </html> The version of the IE i am using is 7.0 and firefox is 3.0.3. I hope some of you would have faced this problem. Any help is highly appreciated. Thanks, --Mayur Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 5, 2008 Share Posted November 5, 2008 is there a reason people don't use JS libraries still? do you choose not to or are you just not aware of the wonders of things like jQuery/Mootools/Prototype? Here is your code simplified and cross-browser compliant with jQuery: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Ajax Demo</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> </head> <body> <h1> An Ajax Demo </h1> <form> <input type = "button" value = "Fetch Message" onclick="$('#targetDiv').load('data.txt');"> </form> <div id="targetDiv"> <p> The fetched message will appear </p> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
mayurpuli Posted November 6, 2008 Author Share Posted November 6, 2008 is there a reason people don't use JS libraries still? do you choose not to or are you just not aware of the wonders of things like jQuery/Mootools/Prototype? Here is your code simplified and cross-browser compliant with jQuery: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Ajax Demo</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> </head> <body> <h1> An Ajax Demo </h1> <form> <input type = "button" value = "Fetch Message" onclick="$('#targetDiv').load('data.txt');"> </form> <div id="targetDiv"> <p> The fetched message will appear </p> </div> </body> </html> Hi, Thanks for the reply and the gyan. Coming to the JS libraries, the later part is correct i am not familiar with the technology first and hence awareness is minimal. The code works fine. It took almost two days for me to come up with some development, may be i am dumb. That's an achievement as a first step for me. I appreciate the help and thanks once again. Let's say if i am rolling out this code to production; Do i need to download and bundle the java script file from the url or referring to url is just fine? What if the url path gets changed tomorrow? --Mayur Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 6, 2008 Share Posted November 6, 2008 i would download and host it locally. chances are, the url will be there forever. and, but letting google host the script, users will be able to get the file faster. but it's so small, you might as well just host it on your server. the big thing is AJAX...it's different for every browser it seems like. using a library saves so much trouble. you are better off putting your energy into creativity then wasting it on making sure the AJAX works... i hope this saves tons of time for you Quote Link to comment Share on other sites More sharing options...
mayurpuli Posted November 6, 2008 Author Share Posted November 6, 2008 i would download and host it locally. chances are, the url will be there forever. and, but letting google host the script, users will be able to get the file faster. but it's so small, you might as well just host it on your server. the big thing is AJAX...it's different for every browser it seems like. using a library saves so much trouble. you are better off putting your energy into creativity then wasting it on making sure the AJAX works... i hope this saves tons of time for you Certainly a great input. Thanks a ton Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.