faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 46 people (48%) answered Yes
Recently 1 of 10 people (10%) answered Yes

Entry

Is there a Javascript only solution (no LiveConnect) to read from a url (i.e. http://www.blah.com/dyna.php) and put its contents in an object?

Apr 5th, 2008 19:12
ha mo, Yar Dmitriev, Andy Sy, Colin Fraser,


Update:
XMLHTTP object (for IE 5+) is what you need. Though, the request is 
limited within the host and the port of the script.
See http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/xmlsdk30/htm/xmobjxmlhttprequest.asp
Example:
<html>
	<div id="divDisplay">The response will be put in here</div>
	<input type="button" onclick="sendData();" value="Send it!">
	<script>
	function sendData(){
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.Open("POST", "http://www.domain.tld/test.php", 
false);
		xmlhttp.Send("<timesheet>An impossibly useless 
timesheet fragment</timesheet>");
		divDisplay.innerHTML=xmlhttp.responseText;
	}
	</script>
</html>
People also point that similar feature named 'webrequest' exists for 
Mozilla.
Thanks to guys from #javascript (EFNET).
====================
This is an interesting question, I have been trying to get a solution to
it since the first time I saw it, and have failed completely. So I am
giving up. I would say that it appears that the limitations of
JavaScript in its file handling capabilities mean this is just not going
to happen for you or anyone else. I am not going to say that this is the
final word here, but it is going to take a much better programmer than
me to find a solution I am afraid. 
I am sorry I could not give a better answer, but someone else may.
====================
I found a way, but it relies on the contentDocument attribute which 
afaik only works under Mozilla, =(. Maybe someone can try if it works 
under IE6 already?  MS is ignoring web standards yet again!
<html>
<script>
<!--
function geturl() {
  tempFrame=document.getElementById("dummyIFRAME");
  tempFrame.src=document.getElementById("url").value;
}
function showcontents() {
  f2=document.getElementById("dummyIFRAME");
  alert(f2.contentDocument.documentElement.innerHTML);
}
-->
</script>
<body>
<input id='url' type='text'></input><br>
<input onclick='javascript: geturl()' type='button' 
value='get'></input><br>
<input onclick='javascript: showcontents()' type='button' 
value='show'></input><br>
<!-- file://c|/PCBodega/avrs1.html -->
<iframe id='dummyIFRAME'></iframe>
</body>
</html>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com