Skip to main content
gamedev.net
🔒 Locked

[web] javascript reading files

Started by walkingcarcass Mar 23, 2005 at 7:10 AM 9 replies 2.8k views
Original Post
walkingcarcass
walkingcarcass
AFAIK &#106avascript can't read files (even from the host server) without ActiveX, which isn't an option for me. Is there any way round this (I dunno maybe open a url in a new hidden window and read its form data, but popup blockers and tabbed browsers will intefere). What I basically want to do is update a page based on a database but without refreshing the whole thing. The only solution I can think of is frames, which I'd prefer to avoid.
spraff.net: don't laugh, I'm still just starting...
Sander
Sander
Take a look at the recent threads in this forum on &#106avascript HTTP requests and &#106avascript XML requests. You could use that (write a PHP script that outputs the requested data as an XML page, parse and update current page through &#106avascript). Other than that I know of no way to load things from the server without refreshing the page.
Michalson
Michalson
If the host doesn't support anything complex, you could always create a second (0 pixel) frame to query the site. How complex things get depend on what your user interaction is. If it's something like periodically updating stock quotes, it's very simple. If you are doing some sort of form submission, it will get harder as you'll have to have a variable to store whether or not the hidden frame is getting information from the server. Once the frame loads, &#106avascript on it will need to report to update the main frame.
a_bertrand
a_bertrand
Without looking arround:

function gettext()
{
var http;
var url;

try
{
http = new XMLHttpRequest();
}
catch (e)
{
try
{
http = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
http=false;
}
}
if(!http)
{
return;
}
http.open("GET", "http://yourwebsite/yoururl");
http.onreadystatechange = function ()
{
if (http.readyState==4)
alert(http.responseText);
}

http.send(null);
}
---http://www.nowhere-else.org
markr
markr
Unfortunately, on IE, the XmlHttpRequest and the XML Document objects are considered to be "ActiveX" objects, so if ActiveX (or ActiveX scripting) are disabled, you're not going to be able to use them.

This is a pity, as these are the only obvious ways of reading file with &#106avascript.<br><br>I suppose in a hacky way you could store the file contents in a .js file as a huge literal string or something, but that's nasty.<br><br>Mark
warhammerdude20
warhammerdude20
what about an iframe? they are way more simple then regular frames.
Zanthos
Zanthos
Steps I took to get past this:

1. Create SCRIPT DOM object
2. Keep reference to DOM object
3. Set URL attribute to something like, http://server/rpc/motd.php
The server will now recieve this request, and respond as a &#106avascript file:
rpcMotd_Set('Woo it worked');

(rpcMotd_Set being a function loaded from a JS file on the client, for setting the MOTD box on the game login page).


I ended up expanding on this method to allow more than one RPC request at any one time, deleting the DOM objects as and when they were completed. The way I did this was to assume a timeout of 10 seconds for any RPC request(plenty of time). Each active script DOM object has a timeOut attribute which is initially at 2, lowered by 1 every 5 seconds, when it reaches 0, the object is deleted.
walkingcarcass
walkingcarcass
I presume Google Suggest must manage it somehow, and without ActiveX since I'm using firefox. Can't see what their code does tho.
spraff.net: don't laugh, I'm still just starting...
markr
markr
When I said you couldn't do it without ActiveX, I meant that you couldn't do it in *IE* without ActiveX.

Of course other browsers allow you to use XML DOM objects and XmlHttpRequest without ActiveX - they have their own, non-ActiveX based versions of these objects.

But if you want to do it in IE with ActiveX object scripting disabled, you can forget it.

Mark
walkingcarcass
walkingcarcass
Is's sorta working, the fetchDB function fetches the database file for the co-ordinates it's given and just dumps it for now. It is called whenever the map is scrolled or refreshed.

Have a look at it now. It's a wierd bug if you check the script souce, the second time fetchDB is called there's an exception, but not the first. What's going on? My only guess is that the HTTP request is asynchrynous and the second one starts before the first finishes. If this is the case, how can I make the code wait without hanging the browser?

Also, it doesn't dump the results until the second time either map is scrolled, which it believes is the first. Maybe the actual first attempt was abandoned because of the exception. ????????
spraff.net: don't laugh, I'm still just starting...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.