...

View Full Version : Including the contents of an external page using Ajax



Kenet
10-09-2008, 01:58 AM
Ok, I have been thru about 10 scripts and this one is the closest to what I'm looking for... it doesn't strip out the javascript of the external files. However on thing that it doesn't do, and I need it to do is allow me to include the external page within a target <div>

Right now all it does is just include a file and I need to be able to call that external file dynamically and place it into a target <div>

Here's the code from http://www.javascriptkit.com/dhtmltutors/ajaxincludes.shtml



function HttpRequest(url){
var pageRequest = false //variable to hold ajax object
/*@cc_on
@if (@_jscript_version >= 5)
try {
pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try {
pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e2){
pageRequest = false
}
}
@end
@*/

if (!pageRequest && typeof XMLHttpRequest != 'undefined')
pageRequest = new XMLHttpRequest()

if (pageRequest){ //if pageRequest is not false
pageRequest.open('GET', url, false) //get page synchronously
pageRequest.send(null)
embedpage(pageRequest)
}
}

function embedpage(request){
//if viewing page offline or the document was successfully retrieved online (status code=2000)
if (window.location.href.indexOf("http")==-1 || request.status==200)
document.write(request.responseText)
}

HttpRequest("external.htm") //include "external.htm" onto current page

Basically I need to be able to call the code like this:
HttpRequest("external.htm, targetDiv")

Can anyone help me?

A1ien51
10-09-2008, 03:49 PM
Wouldn't it be easier to us an iframe in this case?

Eric

Kenet
10-09-2008, 06:11 PM
No because iFrames will not validate since they are no longer part of the HTML standard. I am creating this website in xHTML 1.0 Strict.

I know there has to be a way to do this... I just don't know how to properly write the code to do it.

A1ien51
10-09-2008, 06:26 PM
Well with XHTML1 strict you can use the object tag, onlly issue with that is IE does not support that way so you would have to do conditional comments to make it work.

I am just saying that you are reinventing the wheel by just showing another page inside another. With that comes all of the hassles.

If you were to use a library such as JQuery or Prototype with their Ajax stuff, they have built in functionality to handle scripts that are in the response.

Eric

A1ien51
10-09-2008, 06:31 PM
To answer your question



function HttpRequest(url,divId){



and




change
document.write(request.responseText)
to
document.getElementById(divId).innerHTML = request.responseText;


Personally that code is yucky and you page will have invalid code because the code you are requesting also has html, head, body, etc in the wrong place.

Eric

rnd me
10-09-2008, 10:33 PM
Well with XHTML1 strict you can use the object tag, onlly issue with that is IE does not support that way so you would have to do conditional comments to make it work.
Eric

quick note:

conditional comments (the ones for not ie) also break validation.

a workaround i found is using two if IE condcoms around the opening and closing tag of a style="display:none" textarea.

you can them hide any html except a textarea inside, and it will still validate.

just a technique figured out when i had a webtech prof who was anal about validation.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum