Here's a strange one - parse a .txt file..... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Here's a strange one - parse a .txt file.....

wgtoolcowgtoolco Posts: 1Member
Is there a way using Javascript to parse an external text (.txt) file and create JS variables from the result? For example, If I had a text file with the contents:

"MyName"; 29; 8; 1973;

Could I somehow parse it and create the variables:

var name = "MyName";
var age = 29;
var birthMonth = 8;
var birthYear = 1973;

The script would retrieve the path to the text file from a cookie (I have that part working). In the above example, I used semicolons (;) as delimiters. In reality, I could use virtually anything as a delimiter (including carriage return line feed). Any help would be greatly appreciated, I know a little bit about scripting but this is way over my head.


Comments

  • bgibbybgibby Posts: 251Member
    : Is there a way using Javascript to parse an external text (.txt) file and create JS variables from the result? For example, If I had a text file with the contents:
    :
    : "MyName"; 29; 8; 1973;
    :
    : Could I somehow parse it and create the variables:
    :
    : var name = "MyName";
    : var age = 29;
    : var birthMonth = 8;
    : var birthYear = 1973;
    :
    : The script would retrieve the path to the text file from a cookie (I have that part working). In the above example, I used semicolons (;) as delimiters. In reality, I could use virtually anything as a delimiter (including carriage return line feed). Any help would be greatly appreciated, I know a little bit about scripting but this is way over my head.
    :
    :
    :
    Hi there,

    Try this...

    In your normal script section where you're going to set the cookies create a function called addItem.

    Here's an example.

    [code]





    function addItem(name, age, birthMonth, birthYear) {
    setCookie("name", name);
    setCookie("age", age);
    setCookie("birthMonth", birthMonth);
    setCookie("birthYear", birthYear);
    }











    [/code]

    If you're able to change your output from

    [code]

    "MyName"; 29; 8; 1973;

    [/code]

    to

    [code]

    addItem("MyName", 29, 8, 1973);

    [/code]

    and change the filename to whatever.js instead of whatever.txt then you're fine q:)

    HTH
    Bradley q:)
  • borislavborislav Posts: 16Member
    Hi,
    JavaScript cannot open text files.
    If you want to do this you must use the FileSystemObject.
    Problem is that this is an ActiveX control and as I know is work only on Windows based machines.
    If you successed to create FSO object you can open any file type and the delimiter character can be user defined.

Sign In or Register to comment.