Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Including the contents of an external page using Ajax

    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/dhtmltu...includes.shtml

    Code:
    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?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Wouldn't it be easier to us an iframe in this case?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    To answer your question

    Code:
    function HttpRequest(url,divId){

    and

    Code:
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by A1ien51 View Post
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •