Reputation: 2296
I`m from Russia, so sorry for my bad English.
I want to load the main page of my site by js, and i use this script:
<script type="text/javascript">
function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send(null);
return xmlHttp.responseText;
}
alert(httpGet('http://site.ru'));
</script>
Script located at site.ru/page123.
It works in Firefox and really alert my main page, but if i run it in Opera, nothing happens. Please, fix my code, i can`t see any error in it. Thanks in advance.
Upvotes: 1
Views: 1108
Reputation: 318518
XHR is usually asynchronous (switching it to synchronous mode is not recommended for reasons like freezing the browser). You better use a callback.
Since dealing with XHR manually is annoying, I'd suggest you to use jQuery. Using jQuery your code would look like this (that's just the easiest/most simple way to do it):
$.get('http://site.ru', function(resp) {
alert(resp);
});
Upvotes: 1