|
||
| Inside Technique : Client-side Data Access : Saving Changes to disk It is a fallacy that a Web page cannot write to the client hard disk. ActiveX controls embedded within the page are permitted to do so, but the price they pay for it is of being signed unsafe for scripting. What that means is the user will see this message when the control is initialized by the page :
![]() Fig 2:A nice way to scare the pants off your user At first glance, this looks like the ideal approach if one has to save data on the client disk. Use a signed, though unsafe control, and let the users decide if they want additional features (which require access to the client disk), or safety. Unfortunately, it doesn't quite work. IE provides users with a variety of security options, based on Current Zone (Internet, Intranet, Local) or technology (Script, Java VM). What this means is that a page author has no guarantee whether a user will even see the security dialog, let alone respond to it positively. A less than ideal option would involve the use of cookies. These little tidbits are designed to store and retrieve information from the client disk, but they have too many restrictions applied to them. To begin with, a cookie cannot be longer than 4 KB (4096 bytes), a page cannot hold more than 20 cookies, and, worst of all, even if you set an expiry date, cookies can be randomly deleted by the browser if it finds it is running out of storage space. Cookies also have the problem of failing silently, just setting a cookie is no guarantee that you will be able to retrieve it in the future. The least sophisticated option also happens to be the most robust one, and is guaranteed to work regardless of the user's security settings, or custom browser installation. This approach transfers the burden of saving the data to the user. You would implement such an approach like this :
function dosave(){
newwin=window.open('','','scrollbars=yes,status=no');
newwin.document.open("text/plain");
for(i=0;i<TDC1.recordset.Fields.count-1;i++)
newwin.document.write('"'+TDC1.recordset.Fields(i).name+'",');
newwin.document.writeln('"'+
TDC1.recordset.Fields(TDC1.recordset.Fields.count-1).name+'"')
TDC1.recordset.MoveFirst();
while(!TDC1.recordset.eof){
for(i=0;i<TDC1.recordset.Fields.count;i++){
FldValue=TDC1.recordset.Fields(i).value;
NotNumericFld=isNaN(FldValue);
if(NotNumericFld)
newwin.document.write('"')
newwin.document.write(FldValue);
if(NotNumericFld)
newwin.document.write('",')
else
newwin.document.write(',')
if(i==TDC1.recordset.Fields.count-1)
newwin.document.writeln('')
}
TDC1.recordset.moveNext();
}
newwin.document.close();
}
What this code does is pop up a window, then open a text channel to the new window, then dump the entire contents of the TDC to it. Obviously, before the window is opened, the user has to be told to right-click the document, choose View Source, and when Notepad pops up, choose the File->Save As menu item to save the file as a text file in the appropriate directory. Extremely clunky, yes, but as we mentioned before, the only safe way to ensure that the data changes are written to disk. As a demonstration, we have created a data file which you can alter using the functionality of the TDC. When you attempt to exit this page, you will be asked if you wish to save the changes to the TDC. If you answer in the affirmative, you will see the above scipt in action. Use the gray buttons above the textboxes to manipulate the TDC's data.
Demo requires Internet Explorer 4.0 or later Till the next column then, which will be the final release of the Sokoban game... In the meantime, be sure to read Rajeev's first article that demonstrates recursion to create an interactive maze game.
Page 1:Client-side Data Access © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |