...

View Full Version : How to read an external html file as a string



paulinnorway
11-09-2006, 02:29 PM
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:)

ess
11-09-2006, 03:45 PM
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

paulinnorway
11-09-2006, 06:07 PM
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:thumbsup:.
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.

mrhoo
11-09-2006, 06:19 PM
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.

paulinnorway
11-09-2006, 06:29 PM
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:thumbsup:.
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:confused:.
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;)).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum