Results 1 to 10 of 10

Thread: loading external webpage (different domain) into div

  1. #1
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loading external webpage (different domain) into div

    Hello,

    I am trying to load an external webpage from a different domain into a div.

    I tried using a iframe, and use the innerHTML property to get the contents
    and print it out, but it doesn't work, due to the cross domain restriction.

    Is there any way to do this? (client side)

    Thanks,
    jimhap

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    An iframe should work, it should be something like this:
    Code:
    <iframe src="http://www.google.com/">Google</iframe>
    If you want more help:
    Please post a link to the page on your site that contains the problematic script or attach your code so we can check it out.

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You could try using PHP: File_get_contents:
    Code:
    $site = "http://dyanmicdrive.com";
    echo $file_get_contents($site);
    Jeremy | jfein.net

  4. #4
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Snookerman View Post
    An iframe should work, it should be something like this:
    Code:
    <iframe src="http://www.google.com/">Google</iframe>
    If you want more help:
    Please post a link to the page on your site that contains the problematic script or attach your code so we can check it out.
    WOW... that was fast...

    Here is my HTML:

    HTML Code:
    <div name="iframe" id="iframe"></div>
    <iframe id="foo" name="foo" src="http://www.w3schools.com/"></iframe>
    and my Javascript is:

    Code:
    document.getElementById('iframe').innerHTML = frames['foo'].document.body.innerHTML
    What I am trying to do is either:

    1. Edit the innerHTML of the iframe...
    OR
    2. Get the innerHTML of the iframe and place it in the DIV. (as you see above in my JS)

    So far, I've only heard that there could be a way to read the iframe's HTML,
    but not on all browsers...

    Thanks,
    jimhap

  5. #5
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Why not just have the iframe?

  6. #6
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    You could try using PHP: File_get_contents:
    Code:
    $site = "http://dyanmicdrive.com";
    echo $file_get_contents($site);
    I said client, not server side.

    It would work - IF external URL loading is enabled (php.ini), which isn't for my web host.

  7. #7
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Snookerman View Post
    Why not just have the iframe?
    Since I would like to alter the contents inside the iframe....

    More specifically, to load another page and edit a div's contents in the iframe.

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Why not replace:
    Code:
    document.getElementById('iframe').innerHTML = frames['foo'].document.body.innerHTML
    With:
    Code:
    document.getElementById('iframe').innerHTML = document.getElementById('foo').document.body.innerHTML;
    Jeremy | jfein.net

  9. #9
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Why not replace:
    Code:
    document.getElementById('iframe').innerHTML = frames['foo'].document.body.innerHTML
    With:
    Code:
    document.getElementById('iframe').innerHTML = document.getElementById('foo').document.body.innerHTML;
    Hello,

    Firefox (more precisely, Firebug) reports:

    Code:
    document.getElementById("foo").document is undefined
    Is there anywayto get this to work on ALL browsers?

  10. #10
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jquery & Ajax

    Quote Originally Posted by jimhap View Post
    Since I would like to alter the contents inside the iframe....

    More specifically, to load another page and edit a div's contents in the iframe.
    try using jquery & ajax to load content upon action taken.

    create your site as normal and create a link that calls the content you want to display. make sure you either return false or preventDefault() method to avoid content loading in external page.

    //Atach an event to the link tag with a class of "next".
    $("a.next").live("click",function(){
    //get the address of the page to load.
    var page=$(this).attr("href");

    //This function will be called when the data has loaded.
    function doSomethingWithData(data) {

    //Select only the content inside the new pages #content div.
    var newcontent=$(data).
    find("#content");

    //Insert the content into this pages #content div.
    $("#content").
    html(newcontent);
    }

    //load in the new page using AJAX.
    $.get(page, doSomethingWithData);

    //prevent the browser from performing the default link action.
    return false;
    });


    now use the following in the head of your page to call the above script.

    $('#content').load('mypage.html',function() {
    //any code here runs once content has loaded.
    });

    this will require you to download jquery and upload to server but i think best method of calling in external content as completely cross browser.

Bookmarks

Posting Permissions

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