How to load file (from URL) and print it using javascript

Discussion in 'JavaScript' started by lusiano, Feb 27, 2008.

  1. #1
    Hello Javascript master,

    Can I ask some question?
    I have 2 website, 1 website is almost run out of bandwidth, I don't want to upgrade and I have an idea to share bandwidth between my website.

    My question is, how can I -- print source from a file located at some website -- and show it at client browser side using javascript.

    I know how to do this using PHP, but PHP is server side script, and thats why PHP cannot save my bandwidth using this method. I need to use javascript, so visitor can directly load the content from my other website.

    this is the code if i using php

    <?
    $a = file_get_contents("http://mywebsite/code_file");
    echo $a;
    ?>
    PHP:
    How to do it using javascript? This is my idea using javascript

    <html>
    large_html_data_no_1
    <script type="text/javascript">
    <!--
    document.write(http://mywebsite/code_file);
    // -->
    </script>
    </html>
    Code (markup):
    inside hxxp://mywebsite/code_file is

    "large_html_data_no_2"

    the result is :

    <html>
    large_html_data_no_1
    large_html_data_no_2
    </html>

    when my visitor load my website.

    How can I do this using javascript?

    Thank You.
     
    lusiano, Feb 27, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Bandwidth will be used whether you use PHP or JavaScript.
     
    MMJ, Feb 27, 2008 IP
  3. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Why don't you use iframes? I know they are bad for spiders but accessibility-in-general but is the content such that it can do without it?
     
    rohan_shenoy, Feb 27, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This won't solve the bandwidth problem.
     
    MMJ, Feb 27, 2008 IP
  5. lusiano

    lusiano Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well its ok if this will not solve the bandwidth problem, please anyone know how to do it? or point to me where to read?

    Javascript is new to me, i like to learn it by making code with it.

    Thanks.
     
    lusiano, Feb 27, 2008 IP
  6. lusiano

    lusiano Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Shorter question.

    How do I read a file (located at remote URL) into a JavaScript variable and print it?

    I know hot to print it, using document.write, but i don't know how to read a file (located at remote URL) and insert it to JavaScript variable.

    Have any idea?

    Thank You
     
    lusiano, Feb 27, 2008 IP
  7. blacknet

    blacknet Active Member

    Messages:
    709
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    70
    #7
    you can't, your needing XMLHttpRequest, but XMLHttprequest only works on the same site not for remote sites.

    you'll need to use an IFrame as suggested OR in php header('Location: http://remoteUrl'); which will redirect them.

    more sensible approaches would be to change your hosting/isp/package :)
     
    blacknet, Feb 27, 2008 IP
  8. lusiano

    lusiano Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hmmm to bad.... Thank you for the reply
     
    lusiano, Feb 27, 2008 IP