how do you dynamically load html

Discussion in 'OT Technology' started by RiKuN, Feb 3, 2007.

  1. RiKuN

    RiKuN New Member

    Joined:
    Sep 20, 2006
    Messages:
    37
    Likes Received:
    0
    how do you load text from another website on to your website? say there's only a portion of another website that you want, and it's in div tags with a certain id. what i want is to load that text onto my page. i know javascript has a function getElementbyid, but i think that's only for text on the same page.
     
  2. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Your best bet is to use a server-side language like PHP or Perl to do this. Any way you do it would be inefficient though since you'll have to load the entire page everytime you want that little piece. Javascript *might* be able to do it with an iframe and getElementById but like I said, no matter what, inefficient.
     
  3. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    XMLHttpRequest
     
  4. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    Alter to fit your needs:

    Code:
    var connection;
    var returnText;
    function getData(mode,url)
    {
     connection = createXmlHttpRequestObject();
     returnText = null;
     
     if(connection)
     {
      try{
       connection.open(mode,url,false);
       connection.onreadystatechange = handleRequestStateChange;
       connection.send(null);
          }catch(e){ alert("Can't connect to server:\n" + e.toString()); }
     }
     return returnText;
    } 
    function createXmlHttpRequestObject()
    {
     var xmlHttp;
     try {
      xmlHttp = new XMLHttpRequest();
     }catch(e){
      var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
               "MSXML2.XMLHTTP.5.0",
               "MSXML2.XMLHTTP.4.0",
               "MSXML2.XMLHTTP.3.0",
               "MSXML2.XMLHTTP",
               "Microsoft.XMLHTTP");
     
      for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
      {
       try {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
       }catch(e){}
      }
     }
     if(!xmlHttp)
       alert("error creating the XMLHttpRequest object.");
        else
       return xmlHttp;
     
     return null;
    }
    function handleRequestStateChange()
    {
        if (connection.readyState == 4)
     {
      if(connection.status == 200)
      {
       try{
        returnText = connection.responseText;
       }catch(e) { alert("Error reading the response: " + e.toString()); }
      }else
        alert("There was a problem retrieving the data:\n" + this.statusText);
     }
    }
    
     
  5. mace

    mace i don't read

    Joined:
    Jun 6, 2004
    Messages:
    12,567
    Likes Received:
    0
    Location:
    Texas
    does not work outside of the site the javascript is hosted on
    same-origin policy

    the only way to do this would be a server side script
     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Wow. I did not know that.
     
  7. RiKuN

    RiKuN New Member

    Joined:
    Sep 20, 2006
    Messages:
    37
    Likes Received:
    0
    Thanks guys. I'll give them a try.
     

Share This Page