views:

49

answers:

2

Hi

this is my code for taking external page into div using ajax what i tried is i clicked on button i must display the response in div but i tried several times but doesn't works. my javascript code is

var rootdomain="http://"+window.location.hostname

function ajaxinclude(url) {
    var url=rootdomain+url;
    alert(url);
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.open('GET', url, false) //get page synchronously 
page_request.send(null)
writecontent(page_request)
}

function writecontent(page_request){
if (window.location.href.indexOf("http")==-1 || page_request.status==200)
document.getElementById("eee").innerHTML=(page_request.responseText);
}

and this is my body section :-----

<input type="button" onclick="ajaxinclude('/songcake/index.php')" value="Click !" />
<div id="eee" style=" width:400px; height:800px;">
</div>

please help

Thanks.

+2  A: 

Use jQuery and you can just do something like

$.get('/songcake/index.php', function(data) { $("#eee").html(data); });
Craig
Even if I agree you, do you really think that a 20kb+ library is appropriate only for ONE AJAX call? :)
Ionut Staicu
@lonut: Why simply reference jQuery from the Google CDN? That way the script will nearly always have been cached before even arriving at the site?
Brian Scott
When it's jQuery yes.
Graphain
+1  A: 

attach your method to onreadystatechange which not there in your code

 page_request.onreadystatechange = writecontent;

 function writecontent() {
   if (page_request.readyState != 4)  { return; }
   document.getElementById("eee").innerHTML=(page_request.responseText);

 }
Pranay Rana
I don't think innerHTML is supported on all browsers.
Craig
but how should i include this code in my page so that it should work.....
rajesh
writecontent(page_request) replace this line with page_request.onreadystatechange = writecontent; and copy the function pasted by me in your code
Pranay Rana
thanks for the response but its doesn't work properly.actually when i entered in my code it doesn't work ,please suggest solution.
rajesh
chek this example : http://www.xul.fr/en-xml-ajax.html
Pranay Rana
hey thanks for the reply but will u please ur gmail id so that while chatting i can clear idea...thanks
rajesh