Click to See Complete Forum and Search --> : how to read a web page content to a variable


wings1234
11-20-2003, 03:12 AM
Is there a way to read the content of a web page to a variable using javascript, but don't open that web page?
Somthing like PHP's:

$url="http://www.somesite.com"
$content = join('', file($url));

Or other client-side script can?

gil davis
11-20-2003, 06:13 AM
There are security measures on client-side that prevent you from manipulating files. If you do not "load" the file, you cannot get to the data. Also, if it is a file in another domain, you cannot even get to the data.

fredmv
11-20-2003, 06:19 AM
You could use XMLHTTP to do it. Here's an example:<script type="text/javascript">
//<![CDATA[
function sendRequest(url)
{
var response = null;
var connection = new ActiveXObject("Microsoft.XMLHTTP");
try
{
connection.open("GET", url, false);
connection.send();
if(connection.readyState == 4) response = connection.responseText;
}
catch(e)
{
alert("ERROR: The remote host could not be found or the connection was refused.");
return false;
}
alert(response);
return false;
}
//]]>
</script>
<form action="#" onsubmit="return sendRequest(elements[0].value);">
<div>
<input type="text" style="width: 250px;" value="http://www.google.com/" />
<input type="submit" value="Send GET Request" />
</div>
</form>That, however, will only work in IE since it uses ActiveX scripting objects, but could be modified quite easily to work in Mozilla.

SnowCrash
11-20-2003, 08:12 AM
In Mozilla it's just "new XMLHttpRequest()" instead of "new ActiveXObject("Microsoft.XMLHTTP")".

Works very well (but only if you have XmlHttp installed...). You can read files via JavaScript without reloading the page.

I'm writting an Arkanoid clone (http://www.speich.net/computer/blockedup.php) in js that uses XmlHttp to read and write the highscores. View source to learn more.

fredmv
11-20-2003, 08:28 AM
That's absolutely amazing. Awesome work.

SnowCrash
11-20-2003, 08:32 AM
Thanx for your feedback, fredmv. My Ego likes to hear that :-)

fredmv
11-20-2003, 08:36 AM
You're quite welcome. You are seriously a God of JavaScript. I consider myself to know JavaScript fairly well, but I don't think I could write that as of now. I need to learn more about object collision detection and the math concepts behind it. That game is just so well done. Another impressive thing about it is that you didn't even use a JavaScript game API, you wrote the whole thing yourself. Again, great, great work.

wings1234
11-29-2003, 05:44 PM
Thanks a lot for your code. I tried it, it worked when I used it in my local computer, but when I uploaded it to the server, it always show "ERROR: The remote host could not be found or the connection was refused."

SnowCrash
11-30-2003, 05:08 AM
If you use Xmlhttp from JavaScript (on the client-side) and not from JScript, VBScript or something on the server-side, it doesn't make a difference if you test your script locally or only after you uploaded it, because the code is always executed on your machine (your machine connects to the url indicated in the open command). This means that there must be a connection problem (host down or something) or you did a typo ?.

Whereas you use Xmlhttp on the server-side, it could be that your provider doesn't allow you to connect from your domain to another domain not hosted by themselfes.

wings1234
11-30-2003, 07:36 AM
I tried two different servers. When I used it locally in my computer, it works fine. However, when I uploaded it to servers, it can only access files in the same server. When I tried to connect to other servers, it showed permission denied. By the way, they are all linux servers.

SnowCrash
11-30-2003, 08:30 AM
Sorry, but I can't help you any further, cause I'm not a server admin.

ChangedSoul
02-03-2004, 10:13 PM
Wow, that code snipet is awsome. works great. Will this code also work with Netscape? Now I just need to know how to sift through the code and extract the text I need from what that code returned. Any clues as to how one might go about that? I'm a newbie so bare with me :) maybe somthing like a Instr function like in VB? Thanks for th help.