...

View Full Version : Newbie question



peterbi
05-12-2009, 09:38 PM
Hi,

I almost know nothing about js, but I happen to need to write a simple js function to read .txt file.

Here is the code I have (the function is from internet):


function readit(fileName) {
var data = "";
var fileObj = new ActiveXObject("Scripting.FileSystemObject");
if (fileObj.FileExists(fileName)) {
var txtStream = fileObj.OpenTextFile(fileName,1);
data = txtStream.ReadAll();
txtStream.Close();
}
return data;
}

alert (readit("file://L:\info.txt"));


I name the file as myfile.js, when I double click the file, it gives me Microsoft JScript runtime error to 'alert()' call:


Error: Object expected
Code: 800A138F

When I googled, I found 'alert()' is kind of standard js call, and I can't see what I missed in my file.

Can anybody help me out?

Another question: although I am not there yet, but I need to have a xml file to call myfile.js, however, when I tried that, I got 'ActiveXObject not defined' error to the following line:


var fileObj = new ActiveXObject("Scripting.FileSystemObject");

I checked I have MS XML 4 installed and I am using IE, with 'medium' security level set, how can I fix it?


Thanks,
Peter

Old Pedant
05-12-2009, 11:15 PM
You need to serve the page via IIS, not just by clicking on the HTML file name in windows explorer.

That is, you'll need to do something like
http://localhost/readfile.html

Even doing this, you'll need to answer the security question to allow the loading of that activeX object.

Also, you can *NOT* use a "protocol" as part of the file name when using the FileSystemObject; in place of


alert (readit("file://L:\info.txt"));

you will need to use

alert (readit("L:\info.txt"));
ALSO: you *probably* won't be able to use "L:" if that's actually a link to a directory on another machine. You'll probably have to use the actual path to the directory.
FINALLY: You must DOUBLE UP the backslashes when you use them in JavaScript! \i has special meaning.

I put this code into my file c:\inetpub\wwwroot\junk7.html


<script>
function readit(fileName)
{
var data = "file does not exist";
var fileObj = new ActiveXObject("Scripting.FileSystemObject");
if (fileObj.FileExists(fileName))
{
var txtStream = fileObj.OpenTextFile(fileName,1);
data = txtStream.ReadAll();
txtStream.Close();
}
return data;
}

alert (readit("C:\\inetpub\\wwwroot\\junk7.html"));
</script>

I then used the URL http://localhost/junk7.html and allowed the ActiveX control to run (answered yes to the security question) and it all worked. The alert popped up showing the source code above.

peterbi
05-12-2009, 11:41 PM
Thanks for your reply.

First of all, the file is not .html, it's .js.

I now changed the file path to my local C: drive, it's still not working. I got the same error on calling 'alert()'. I even put a line 'alert("test");' and it still errored at calling it. It seems some how 'alert()' call is not resolved.

Following is the modified code, try to save it as a .js file and see if you can execute (by double clicking) it.



function readit(fileName) {
var data = "";
var fileObj = new ActiveXObject("Scripting.FileSystemObject");
if (fileObj.FileExists(fileName)) {
var txtStream = fileObj.OpenTextFile(fileName,1);
data = txtStream.ReadAll();
txtStream.Close();
}
return data;
}

alert (readit("C:\\info.txt"));

Thanks.

Old Pedant
05-13-2009, 01:07 AM
You mean you want to use Windows Scripting Host--WSH--to run this?

Well, no wonder it doesn't work!

The *method* alert is actually a short form of window.alert( ) and that is a method on the window object *IN THE BROWSER*.

You can *NOT* use a window object--and thus can't use alert--in WSH code.

Go read the docs on WSH and the WScript.

I used this file, saved as C:\inetpub\wwwroot\junk7.js


function readit(fileName)
{
var data = "file does not exist";
var fileObj = new ActiveXObject("Scripting.FileSystemObject");
if (fileObj.FileExists(fileName))
{
var txtStream = fileObj.OpenTextFile(fileName,1);
data = txtStream.ReadAll();
txtStream.Close();
}
return data;
}

WScript.Echo( readit("C:\\inetpub\\wwwroot\\junk7.js") );

And then used a DOS prompt to do:


> cd C:\inetpub\wwwroot
> wscript junk7.js
> cscript junk7.js


Try it.

Your object not found error was for the alert( ) method, not the other object/functions.

peterbi
05-13-2009, 01:52 AM
Hi Old Pedant,

You are right, once I replaced 'alert' with 'WSH.Echo', it works!

Now I have a further question, hope you can help me.

The .js file will be called by my Widget file (an xml file) to display text from a txt file. Idealy the .js file can be a cross-platform file so it can be called on both Windows and Mac, obviously the current one is only for Windows.

Do you know if .js file can be corss-platform? If not, what format should I use on Mac? And if possible, can you provide the Mac compatible file that is equivalent to the .js file?


Thanks in advance.

shedokan
05-13-2009, 01:53 AM
peterbi:
I seee that you are using functions that only work in IE, please don't because there are a lot of users using other browsers and your script will not work in them.

in some countries there are more than 50% users using firefox(big countries).

Old Pedant
05-13-2009, 02:21 AM
So far as I know, the ability to use JS (and it's really JScript--Microsoft's dialect--not true JavaScript) as a system scripting language is limited to Windows.

Note that it *need* Windows Script Host (WSH) to run. Note how I used
wscript junk7.js
cscript junk7.js
??

If you simply use
junk7.js
as a command, it defaults to "wscript junk7.js".

And so far as I know there is no "wscript" or "cscript" equivalent on the Mac. Or Linux. Probably nobody thinks it is needed there because the command scripting language is much much richer than MS's DOS command set.

No, I'm not a Mac user. And been too long since I wrote shell scripts.

Old Pedant
05-13-2009, 02:23 AM
Shedokan: PLEASE actually *READ* the posts here.

He is *NOT* coding for a browser, at all. Your comment is irrelevant.

Granted, his code will only run on Windows. But it *will* run on all Windows machines, because it is NOT dependant on any browser.

shedokan
05-13-2009, 04:32 AM
Ow I'm sorry, I never met this kind of thing.

Old Pedant
05-13-2009, 04:44 AM
Yes, you can actually write some fairly sophisticated scripts using *either* JScript or VBScript. Microsoft use many scripts like this for setting up various parameters, such as those for the IIS web server.

The scripts can't do much with windows, just pop up messages and the like, but they are great for command line scripting.

I think, quite honestly, that MS developed WSH just because the DOS command language is so crappy, especially when compared to the various available Linux/Unix "shell" languages.

adios
05-13-2009, 06:14 AM
... from the stickies:


1) Type in a subject that summarizes your question!- The fastest way to turn off other members wanting to help you is to type an incomplete or silly subject for your post. Examples of poor subject titles include:

- "Help!"
- "I'm a newbie...please!"
- "Is this possible using JavaScript?"
- "loops"
- "Urgent...deadline tomorrow!"
- "A challenge for you JavaScript masters"

The above subjects either have absolutely nothing to do with the question itself, or are grossly incomplete (ie: "loops"). When asking for help, enter a subject that summarizes your question, period! Don't use silly, incomplete, or "bait" subjects.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum