Kurita Kinoshita wrote:
I'm trying to find a way to have a script communicate with a server
without having to load a new page. The idea is to have the server act
as a simple wrapper for a database, letting the client do that icky UI
stuff.
I tried the obvious, putting a frame somewhere it doesn't insult the
eye, changing its location, then accessing the new content via DOM.
What I get from IE (6) for my pains is just a rude "permission
denied". This even though I entered "localhost" as a trusted site in
IE's security setting. What's amiss?
Help from one of you pros out there would be greatly appreciated! ;-)
TIA,
Kurita
ci***********@gmx.net
There are several options that can be used. The choice depends on the
amount and type of data that is being sent back and forth.
1. Loading file via XMLHTTP. The following function will return any type
of text data that you parse with client side script. The filename you
pass is the name of your server side script and can contain query string
that allows you to pass limited amount of data to the server:
function getFile(filename)
{ oxmlhttp = null;
try
{ oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e)
{ try
{ oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ return null;
}
}
if(!oxmlhttp) return null;
try
{ oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e)
{ return null;
}
return oxmlhttp.responseText;
}
2. Similar to the method above you can dynamically create a script node:
function loadJS(filename)
{ scriptNode = document.createElement('script');
scriptNode.src = filename;
scriptNode.type = 'text/javascript';
(document.getElementsByTagName('head'))[0].appendChild(scriptNode);
}
Again filename is your server side script and can contain query string
as well. The difference is that the server side script has to return
javascript (make sure you set the MIME type header to
"text/javascript"). Compared to the first method you receive data ready
to be used.
3. If you have siginificant amount of data to pass to the server, you
would need a hidden iframe so that you can use POST method.
--
Vladdy
http://www.klproductions.com