This should work (I don't have time to test it right now.)
function ajaxFunction() { //Added open {
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Removed additional try / catch
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.write(ajaxRequest.responseText);
document.myForm.time.value = ajaxRequest.responseText;
}
}; // Added semi-colon to the end of the anonymous function definition
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null);
}
A few notes:
- White space is not required for the most part in Javascript, but proper indentation makes it much easier to spot syntax errors.
- When you bind an attribute to an anonymous function you need to follow your
}
with a ;
- Once you understand how this works, dig into one of the larger libraries ajax functions / modules. (Learning is always a good thing, and ajax is one of those areas that really needs a few dozen man-hours of work to encounter all the differences between browsers.)
+
ADDENDUM:
Cross-domain ajax requests are very difficult to do right (i.e. safely, securely, and without throwing errors) -- they are forbidden to javascript directly by the Same-Domain Origin policy.
See this question and this one for more discussion on the subject and ways to get around it with a proxy or with jsonp
+
jQuery's ajax function is 325 lines long (and that's not counting $.ajax.settings
or $.extend()
)