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
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow How to read an external html file as a string

    Hi,

    Does anyone know a short script that will read an external webpage as a string. Then I can manipulate that string to get information that I require. I'm new to JavaScript so I'm not sure about what it can and cannot do yet.
    I'm not really too bothered about compatibility with other browsers, as long as it works with IE7.
    I've done several Google searches with little success, so I was hoping one of you could help me or point me in the right direction.

    Thanks

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    I am not sure that you can just use JavaScript to read a page from another URL. JavaScript is a client side technology which means that you can manipulate objects (i.e. Strings, Dates) once a page has been downloaded to the client's machine. Note that the objects must exist within the page that has just been downloaded.

    However, that does not mean you can not achieve what stated above. You should still be able to do it, with little help from a server side programming language...such as PHP, ASP, JSP...etc

    Here is an example in PHP...which reads a page from a url, and then dumps the data to a variable ready to be manipulated in JavaScript.

    <?php
    echo "<script type='text/javascript'>";
    $contents = file_get_contents( "http://url_goes_here.com" );
    echo "var my_contents = " . $contents;
    echo "</script>";
    ?>

    Good luck
    Ess

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ess View Post
    I am not sure that you can just use JavaScript to read a page from another URL. JavaScript is a client side technology which means that you can manipulate objects (i.e. Strings, Dates) once a page has been downloaded to the client's machine. Note that the objects must exist within the page that has just been downloaded.

    However, that does not mean you can not achieve what stated above. You should still be able to do it, with little help from a server side programming language...such as PHP, ASP, JSP...etc

    Here is an example in PHP...which reads a page from a url, and then dumps the data to a variable ready to be manipulated in JavaScript.

    <?php
    echo "<script type='text/javascript'>";
    $contents = file_get_contents( "http://url_goes_here.com" );
    echo "var my_contents = " . $contents;
    echo "</script>";
    ?>

    Good luck
    Ess
    Thanks for the reply.
    If nobody else can show me a way to do it using just JavaScript then I'll use your way because it sounds exactly like what I'm after.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You can get any file's content with the respnseText from
    an XMLHttpRequest (ajax) call to the URL, or you can use an iframe
    and get the contentWindow.document.documentElement.innerHTML.

    Both of these methods require that the URL you are looking at has the
    same server/domain as the page doing the looking.

    To parse offsite content you need server processes.

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mrhoo View Post
    You can get any file's content with the respnseText from
    an XMLHttpRequest (ajax) call to the URL, or you can use an iframe
    and get the contentWindow.document.documentElement.innerHTML.

    Both of these methods require that the URL you are looking at has the
    same server/domain as the page doing the looking.

    To parse offsite content you need server processes.
    Thanks for the reply.
    Since I need information from a website outside my domain, it seems like I'll have to learn JavaScript and PHP. I know a little about JavaScript and nothing about PHP.
    I guess I'll start trying to learn PHP first because I only need to use JavaScript to manipulate the acquired string, which seems easy enough (I hope).


  •  

    Posting Permissions

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