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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    - Javascript editing .txt file -

    Hello all, I am new to javascript and I have run into a problem. I cant seem to be able to write to a text file called users.txt

    Here is my code:

    Code:
    <html>
    <head>
    <script language="javascript">
    
    
    function rf()
    {
    
        var fs,file;
    
        fs = new ActiveXObject('Scripting.FileSystemObject');
    
        file = fs.OpenTextFile('C:/Users/Imran/Documents/Imran          IT/Web/Test/user.txt',2);
    
        file.Write('The text to write to file');
    
        file.Close();
    
    }
    </script>
    </head>
    <body onLoad="rf();">
    
    </body>
    </html>
    What I do is I put the code in notepad, I then save it as a .html file and try run it in IE 7, google chrome and firefox, Each time users.txt is unchanged.

    Any help appreciated.

    All I want is for the client to send a variable to the server and for that variable to be saved in a .txt file.

    If there is another way to do that, please share

  • #2
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone ?

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    for security reasons javascript is not allowed to create, modify or delete any file.

    It can only read files.

  • #4
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there any way a client can write to a file at all... Any code examples ?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

    Internet Explorer's JScript (only) lets you write to the Windows file system by means of the harmless execCommand() function. It is possible in IE in certain circumstances to use ActiveX objects to access the directory and read files. This is not recommended for a variety of reasons.



    Quizmaster: Which African country gives its name to the complaint known as 'gippy tummy'?
    Contestant: Venezuela.

  • #6
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [QUOTE=Philip M;1059299]JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

    Internet Explorer's JScript (only) lets you write to the Windows file system by means of the harmless execCommand() function. It is possible in IE in certain circumstances to use ActiveX objects to access the directory and read files. This is not recommended for a variety of reasons.

    Thanks for the reply, Its well appreciated.

    How would I print to a text file using AJAX ?

    Any code examples for that ?

    I have tried this with no luck...

    Code:
       
    
    http.open('get','newcontent.txt');
       http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       http.setRequestHeader("Content-length", DATA.length);
       http.onreadystatechange = updateNewContent;
       http.send("hello");

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    [QUOTE=ImranP;1059395]
    Quote Originally Posted by Philip M View Post
    JavaScript
    How would I print to a text file using AJAX ?
    See my answer in post #5. You can't.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,412
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by ImranP View Post
    How would I print to a text file using AJAX ?

    Any code examples for that ?
    you need an http server. IIS comes with winXP/pro and lets you load and save files using ajax's GET and PUT methods. Apache/php can be setup to handle PUT requests as well.

    in HTML5, forms can use PUT as their method, so you can save files without even using javascript!
    (although, what supports html5 but not JS is beyond me...)

    see my demo "Loading and Saving files with JavaScript" for code.
    Last edited by rnd me; 03-01-2011 at 05:50 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Tags for this Thread

    Posting Permissions

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