thanks walkerke... but I need to get html source in javascript as stated above.
Main Topics
Browse All TopicsHey
How can I retrieve the source code from a site in javascript? for example if i wanted to retrieve source code from google.co.uk?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
u cant - javascript allows client side file handling (to some degree) but not remote file I/O.
What you could do is write php to generate javascript that has the content of the html file you want...
i.e.
<?php
?>
<script language=javascript>
function get_the_file ()
{
var filecontents;
<?
$fp = fopen ( 'http://whereever.com/what
while ( $str = fgets ( $fp ) )
{
?> filecontents += "<?=str_replace ( '"', '\"', $str )?>\n"; <?
}
?>
alert (filecontents);
}
</script>
<?
?>
That may help (depending on what ur actually doing)
code not tested or anything -let me know if you need more help - but u certainly cant do it in JS... sorry :()
well i just need to get the html source code of a site that isnt on the same domain so i can process the source code with a regex... javascript doesnt have sockets (as far as i know?)... i thought about vbscript but this probably has more security restrictions than javascript... flash/actionscript possibly an option? im stuck for ideas to do this...
there must be a simple way? :S
do u have control of the remote domain or is it any domain?
if u control the remote as well, you could add a wrapper script that generates any file as a javascript variable string then simply include that wrapper file as a source javascript file and your away... that is of course if you have control of the other domain.
i.e on ur remote host, write a php script that...
takes a filename as the variable in $_GET - so...
www.yourremotedomain.com/w
wrapper.php opens "myfile", encases it in a JS variable (like i have done in my first example) and spits out raw JS.
then you can call the script like so...
<script src=www.yourremotedomain.c
which will return javascript, which you can then do ur regex on.
just found this... uses some ajax something or rather... never heard of it myself, but might help
http://www.devarticles.com
n1875621 is right. You can do this using AJAX.
Basically, AJAX will do a remote call to a server that you specify (from Javascript, in the background). Typically this is used to query servers and get information from databases, but if you query a page the call will return the HTML of that page.
This will get you started:
http://rajshekhar.net/blog
thanks guys for ur help
at the moment i found a site out of pure luck http://www.aspfaq.com/show
I change to:
Response.Write(xmlhttp.res
alert(xmlhttp.responseText
hmm? :S
Most newer version of the major browsers support AJAX. It WILL be the web of the future, which is why they call it Web 2.0. It's good stuff and makes web development more powerful. Just don't over-do it!
See this link:
http://developer.ebusiness
Here is a quick program that does what you want. Save this as an HTM file and run on your computer. Then you can take the parts out that you need. Good luck!
<html>
<head>
<script type="text/javascript">
var xmlHttpRequestHandler = new Object();
var requestObject;
xmlHttpRequestHandler.crea
var XmlHttpRequestObject;
if(typeof XMLHttpRequest != "undefined")
{
XmlHttpRequestObject = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
var tryPossibleVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp",
"Microsoft.XMLHttp"];
for(i=0;i<tryPossibleVersi
{
try
{
XmlHttpRequestObject = new ActiveXObject(tryPossibleV
break;
}
catch(xmlHttpRequestObject
{
// Ignore Exception
}
}
}
return XmlHttpRequestObject;
}
function getHtml()
{
var url = document.getElementById('u
if(url.length > 0)
{
requestObject = xmlHttpRequestHandler.crea
requestObject.onreadystate
requestObject.open("Get",u
requestObject.send(null);
}
}
function onReadyStateChangeResponse
{
var ready, status;
try
{
ready = requestObject.readyState;
status = requestObject.status;
}
catch(e) {}
if(ready == 4 && status == 200)
{
alert(requestObject.respon
}
}
</script>
</head>
<body>
<b>Enter Website URL:</b><br />
<input type="text" id="url" size="50" />
<br /><br />
<input type="button" onclick="getHtml();" value="Get HTML" />
</body>
</html>
How did you type in the address?
It has to be http://www.site.com
This is using HTTP to do the call, so if you do not specify the URL correctly, it will not work. Also, I've noticed that some sites block these types of calls. I'm not sure how, but they do.
Business Accounts
Answer for Membership
by: walkerkePosted on 2006-06-28 at 12:44:17ID: 17004313
You can view the HTML source code of any page in most browser. In IE select "Source" from the view menu. In Mozilla/Firefox, select "Page Source" from the View menu. In Safari, select "View Source" from the View menu.