Hi, i'm quite new in javascript....
I found a script which reads tha contents of a file stored on the web server and prints it on screen like an include.. i want to modify this script so the contents of the file stored on a javascript variable. the script is the following:
<SCRIPT LANGUAGE="JavaScript">
// new prototype defintion
document.include = function (url) {
if ('undefined' == typeof(url)) return false;
var p,rnd;
if (document.all){
// For IE, create an ActiveX Object instance
p = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
// For mozilla, create an instance of XMLHttpRequest.
p = new XMLHttpRequest();
}
// Prevent browsers from caching the included page
// by appending a random number
rnd = Math.random().toString().substring(2);
url = url.indexOf('?')>-1 ? url+'&rnd='+rnd : url+'?rnd='+rnd;
// Open the url and write out the response
p.open("GET",url,false);
p.send(null);
document.write( p.responseText );
}
</SCRIPT>
and for printing the contets of the file i must write inside the body of my
<script>
document.include('header.inc');
</script>
i just want to strore the contents on a variable and not print them
thanks a lot for your time
Bonker
11-23-2005, 11:28 AM
var yourString = p.responseText;
lateralus
11-23-2005, 11:57 AM
thanks for your help... now i would like to ask
i use var myvar = p.responce.Text inside the function
so i call then the function: document.include('myfile');
i've tried inside another function to store the result like
var result = document.include('myfile');
is it possible?...because nothing is being stored
thnaks once more for your time
Bonker
11-23-2005, 01:23 PM
Then you should use: return p.responseText; inside the include function, otherwise your function doesn't return anything.
Be aware that your variable will only contain a value when the request is finished. It's safer to use asynchronous XmlHttp together with an event see for example http://www.15seconds.com/issue/020606.htm