views:

55

answers:

3

Hi,

The code provided below doesn't show all the content of that page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<script type="text/javascript">

var rootdomain="http://"+window.location.hostname
alert(rootdomain);
function ajaxinclude(url) {
var url=rootdomain+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("write").innerHTML=page_request.responseText;
}

</script>
</head>

<body>
<div id="write">
</div>
<input type="button" value="Submit !" onclick="ajaxinclude('/songcake/index.php');"/>

</body>
</html>

Please Help

Thanks.

+1  A: 

Why you should not use jQuery? You can do this simple as below..

$("#write").load("/songcake/index.php");

[EDITED]

Below you can see the completed code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<script type="text/javascript" src='scripts/jquery.js'></script>
</head>

<body>
<div id="write">
</div>
<input type="button" value="Submit !" 
   onclick="$('#write').load('/songcake/index.php');"/>

</body>
</html>

You can download jQuery from here : http://jquery.com/ The source for my answer you can find here : http://api.jquery.com/load/

Muneer
thanks for the replyBut please will u send the complete code.
rajesh
or indeed any other JavaScript library
seengee
@rajesh....,first you have to add Jquery to your codeI will edit my answer..
Muneer
+2  A: 

You need to add a closure that reacts upon the completion of the document loading process.

page_request.onreadystatechange = function() { 
    if(page_request.readystate == 4) { 
        // data handling here
    } 
} 

As pointed out though, using jQuery will make things a lot easier.

Edit: To clarify, your AJAX call does check for the connection status (request.status), but not for the loading status (request.readystate). Your document probably did not load completely. Here's a reference for the W3.org XMLHTTPRequest API: http://www.w3.org/TR/XMLHttpRequest/ .

Edit2: Btw, an <iframe> element would solve your problem with a lot less code.

Edit 3: Code

function ajaxinclude(url) {
    //...
    page_request.open('GET', url, false) //get page synchronously 
    //<> add onreadystatechange handler
    page_request.onreadystatechange = function() {
        if(page_request.readystate === 4) {
            if(page_request.state === 200) {
               //call function on success
                   writecontent(page_request.responseXML)
            }
        }
    }
    page_request.send(null)
}

Some additions:

  1. if you put your ajax call into the <HEAD> you need to either create the dom elements you want to append data to as they are not available when the runtime runs through (which might lead to a dom error); or you need to add an on dom load event handler.
  2. Synchronous calls are not properly implemented in some browsers and this might lead to errors too.
FK82
actually i don't want to use iframe ,i know use of iframe is very but i actually want to use div so please give suggestion regarding code.....
rajesh
I suggest, loding from Ajax into the same frame is better than IFrame.
Muneer
@ Muneer: I said less code, not better solution. In other words, I agree.
FK82
A: 

try to use FireBug

FireBug show you state of your request. If it 200 and you see that in reqest answer (in firebug) broken data then you should check your index.php script

Falcon