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 5 of 5
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Inserting HTML from an external source?

    Hi all,

    I need to write a bit of javascript that can read the contents of an external file (e.g. http://www.mydomain.com/myscript.php) and insert it into a web page (e.g. http://www.myotherdomain.com/mypage.html).

    I envisage something simple in mypage.html like:

    Code:
      .
      .
      <script language="javascript" type="text/javascript">
        data = js_readfile("http://www.myotherdomain.com/myscript.php");
        document.write(data);
      </script>
      .
      .
    That seems really simple and what I'm asking for (in a particularly long-winded way!) is the javascript equivalent of the PHP function readfile (referred to as js_readfile in the example).

    Clearly it could not be *exactly* like the PHP readfile function or this would create an obvious security risk! I imagine it to be a function which will attempt only to read from qualified sources (i.e. webservers, ftp servers, etc), which are authorised to publish data.

    TIA,
    Mike.
    Last edited by mlse; 11-22-2006 at 06:27 PM.
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #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
    Only way would be to have your serverside page write the data onto the page since it is from a different domain.

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

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    You mean turn mypage.html into mypage.php and stick in a bit of PHP in instead of Javascript? So it's not possible to do it in javascript?
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    Sorry to jump in - he means that javascript cannot do this for security reasons and that if you want this to work, you would have to fetch the content from a different domain using a server side script and then make this content available someplace on your domain. Javascript could then fetch this content from your domain.

    Here are a couple of related links -
    http://www.mozilla.org/projects/secu...me-origin.html
    http://www.xml.com/pub/a/2005/11/09/...d-harmful.html
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ok, thanks for the links, I understand now!
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!


  •  

    Posting Permissions

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