Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Newbie question

  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie question

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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
    Code:
    alert (readit("file://L:\info.txt"));
    you will need to use
    Code:
    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
    Code:
    <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.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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
    Code:
    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:
    Code:
    > 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.

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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).

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Ow I'm sorry, I never met this kind of thing.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    ... 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •