...

View Full Version : Inserting HTML from an external source?



mlse
11-22-2006, 06:53 PM
Hi all,

I need to write a bit of javascript that can read the contents of an external file (e.g. http://www.mydomain.com/myscript.php) and insert it into a web page (e.g. http://www.myotherdomain.com/mypage.html).

I envisage something simple in mypage.html like:



.
.
<script language="javascript" type="text/javascript">
data = js_readfile("http://www.myotherdomain.com/myscript.php");
document.write(data);
</script>
.
.


That seems really simple and what I'm asking for (in a particularly long-winded way!) is the javascript equivalent of the PHP function readfile (referred to as js_readfile in the example).

Clearly it could not be *exactly* like the PHP readfile function or this would create an obvious security risk! I imagine it to be a function which will attempt only to read from qualified sources (i.e. webservers, ftp servers, etc), which are authorised to publish data.

TIA,
Mike.

A1ien51
11-22-2006, 07:05 PM
Only way would be to have your serverside page write the data onto the page since it is from a different domain.

Eric

mlse
11-22-2006, 07:23 PM
You mean turn mypage.html into mypage.php and stick in a bit of PHP in instead of Javascript? So it's not possible to do it in javascript?

CFMaBiSmAd
11-22-2006, 07:44 PM
Sorry to jump in - he means that javascript cannot do this for security reasons and that if you want this to work, you would have to fetch the content from a different domain using a server side script and then make this content available someplace on your domain. Javascript could then fetch this content from your domain.

Here are a couple of related links -
http://www.mozilla.org/projects/security/components/same-origin.html
http://www.xml.com/pub/a/2005/11/09/fixing-ajax-xmlhttprequest-considered-harmful.html

mlse
11-23-2006, 11:43 AM
Ok, thanks for the links, I understand now! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum