AJAX include script

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

AJAX include script

Postby Don_HH2K » Sat 15 Jul, 2006 7:15 pm

I found an AJAX script online that will let me include a page if I request it using the function HttpRequest():

Code: Select all
function HttpRequest(url){
var pageRequest = false
/*@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){
   pageRequest.open('GET', url, false)
   pageRequest.send(null)
   embedpage(pageRequest)
   }
}

function embedpage(request){
if (window.location.href.indexOf("http")==-1 || request.status==200)
   document.write(request.responseText)
}


The embedpage() function is supposed to write the retrieved page (specified using HttpRequest(page URL)). This script does that, except instead of writing the contents of the retrieved page at the location of the script in my HTML source, it gets rid of the rest of the page and loads the retrieved page in its place.

Does anybody know what in this script is causing the rest of the page to get dumped, how I can get the script to write the contents of the received page to the location of the <script> element in question on the page. or of another (client-side) way I can go about doing this other than using an <iframe>?
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.4) Gecko/20060516 Donzilla/1.1 (WML/1.3)
Laptop: HP Compaq nx6325 - Turion 64 X2 @ 2GHz, 2GB DDR2, 100GB HD, ATI Radeon X300, 15" LCD, Seven Pro
Handheld: Palm Treo 650 - Intel PXA270 @ 312MHz, 10MB RAM, 32MB flash, 2.7" LCD, Palm OS 5.4
User avatar
Don_HH2K
Moderator
Moderator
 
Posts: 5112
Joined: Sun 09 May, 2004 3:59 pm

Postby akbash » Sun 16 Jul, 2006 10:51 am

When document.write is used on a completed document, it will indeed replace the entire thing. document.write has the advantage that it's compatible with browsers from the bronze age, but most later browsers can speak a little DOM. To insert new HTML after document construction, I suggest you set up a <div> at the desired insertion point, then set its innerHTML property.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060624 Firefox/1.5.0.5
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby akbash » Fri 21 Jul, 2006 9:49 am

You're welcome.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060624 Firefox/1.5.0.5
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby Don_HH2K » Fri 21 Jul, 2006 4:27 pm

I literally just got around to fixing it with your suggestion; worked like a charm. Thanks, and sorry for not mentioning that earlier.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.4) Gecko/20060516 Donzilla/1.1 (WML/1.3)
Laptop: HP Compaq nx6325 - Turion 64 X2 @ 2GHz, 2GB DDR2, 100GB HD, ATI Radeon X300, 15" LCD, Seven Pro
Handheld: Palm Treo 650 - Intel PXA270 @ 312MHz, 10MB RAM, 32MB flash, 2.7" LCD, Palm OS 5.4
User avatar
Don_HH2K
Moderator
Moderator
 
Posts: 5112
Joined: Sun 09 May, 2004 3:59 pm

Postby akbash » Sun 23 Jul, 2006 1:32 pm

It's a weekend thing, eh? I understand. Apologies for my impatience. Glad to hear it worked.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060624 Firefox/1.5.0.5
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm


Return to Web Design and Page Coding

Who is online

Registered users: Google [Bot], Yahoo [Bot]

cron