Web Analytics Made Easy -
StatCounter using external text files - CodingForum

Announcement

Collapse
No announcement yet.

using external text files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • using external text files

    i'm new to scripting but i've picked up the basics pretty well. i need to know if you can get text out of an external file. i know you can use an iframe or a text/x-scriptlet object (im not bothered about cross-browser compatibility, i only use IE5), but these are only good for dumping html files into a box on the page.
    i really want to be able to read a .txt file from my site and use the data in it in my script. i want to use this for navigation links, to save me having to edit every page on my site whenever i add a new page. the text file would basically include a list of all the pages. i don't just want a list of all the pages on each page, i could easy use an iframe for that - i want next and previous buttons so i need to use script to process the list.
    my programming experience is in simple compiled software, using vb, and bbcbasic before that (heaven forbid!!) so i'm used to just being able to open a file and read its data. can you do this in script programming? how?

    ---
    neil.c
    neil.c

  • #2
    sorry to tell you this neil but javascript cant read externel files. if you wan that type of "depth" then oyu should learn PHP or PERL/CGI. php isnt to hard to learn if you knwo the basics to programming in most languages but you have to have a) a free host with PHP instaled or b) a server installed on your computer
    photoshop too expensive? use the GIMP! www.gimp.org

    Comment


    • #3
      ok. thanks for clearing that up.
      neil.c

      Comment


      • #4
        Javascript can get data from an external .txt file by importing the content as a javascript variable. Visit the following for an example:


        The Gedcom analyser script on that site also uses this technique, but goes into a lot more processing of the imported data using regexps.

        These are technical curiosities however; if you want to import a lot of data from external files I would suggest you use xml.

        Patrick

        Comment


        • #5
          never tought of that, but for reading/writing to files php is still better
          photoshop too expensive? use the GIMP! www.gimp.org

          Comment


          • #6
            ok i looked at that eg and i can see how the txt file is loaded in the src of the first <script> tag. but the syntax of javascript object variables is a bit strange to me (so many dots!) and its hard to see what each of the variables is for in this script. how is the text file actually read? what property of which object?

            here is the script:

            <script type="text/javascript" src="stocks.txt"> //datasource for table
            </script>
            <script type="text/javascript">

            /*created by Patrick Brennan - January 2001*/

            var table = "abcdefghijhijklmnopqrstuvwxyz"
            var a, i, j, k, m, n, x, content, current

            function sort(x){
            x = x.id.charAt(0)
            for (i = 1; i <= records-1; i++){
            for (j = 1; j <= records-1; j++){
            k = j + 1
            if (isNaN(document.getElementById(x + j).innerHTML)){
            if (document.getElementById(x + j).innerHTML.toUpperCase() > document.getElementById(x + k).innerHTML.toUpperCase()){
            a = document.getElementById(x + j).innerHTML
            document.getElementById(x + j).innerHTML = document.getElementById(x + k).innerHTML
            document.getElementById(x + k).innerHTML = a
            for (n = 0; n <= header.length-1; n++){
            if (table.charAt(n) != x){
            m = table.charAt(n)
            a = document.getElementById(m + j).innerHTML
            document.getElementById(m + j).innerHTML = document.getElementById(m + k).innerHTML
            document.getElementById(m + k).innerHTML = a
            }}}}
            else{
            if (parseFloat(document.getElementById(x + j).innerHTML) < parseFloat(document.getElementById(x + k).innerHTML)){
            a = document.getElementById(x + j).innerHTML
            document.getElementById(x + j).innerHTML = document.getElementById(x + k).innerHTML
            document.getElementById(x + k).innerHTML = a
            for (n = 0; n <= header.length-1; n++){
            if (table.charAt(n) != x){
            m = table.charAt(n)
            a = document.getElementById(m + j).innerHTML
            document.getElementById(m + j).innerHTML = document.getElementById(m + k).innerHTML
            document.getElementById(m + k).innerHTML = a
            }}}}}}}

            content = ""
            content += "<h1>Sortable Table - Textfile Datasource</h1>"
            content += "<div align='center'><table cellspacing=0><tr>"
            for (i = 0; i <= header.length-1; i++){
            content += "<th id='" + table.charAt(i) + "0" + "' onmouseup='sort(this); return true'><button hidefocus onmouseup='this.blur()'>" + header[i] + "</button></th>"
            }
            content += "</tr>"
            for (i = 0; i <= records-1; i++){
            j = i + 1
            content += "<tr>"
            for (k = 0; k <= header.length-1; k++){
            current = eval("record" + j)
            content +="<td id='" + table.charAt(k) + j + "'>" + current[k] + "</td>"
            }
            content += "</tr>"
            }
            content += "</table></div>"
            document.write(content)

            </script>
            </head>


            thanks for your help.
            neil.c

            Comment


            • #7
              it's clicked finally. your use of a .txt file confused me into thinking it was a data file, but when i thought of opening the file to check this, i realised it was actually a .js file with the wrong extension. not that it matters to the computer, it just confused me.
              so you can't use a data file as such, be it text or csv format; but you can create a bit of external javascript including the bare minimum i.e. directly filling the arrays.
              your example is fine for its job because the names of the variables don't matter at all. but ideally i want an array for the link titles and a separate array for urls. to do this i could fill the arrays by:

              linktitle = ["page 1", "page 2", "page3" /*etc.*/ ]
              linkurl = ["page1.htm", "page2.htm", "page3.htm" /*etc.*/ ]

              but i will be having more pages than will fit on one line, and it will be hard to find which title goes to which url when i'm editing the data script file.
              i really want to declare each title and url together, separately. this would end up looking like:

              linktitle[0] = "page 1"
              linkurl[0] = "page1.htm"
              linktitle[1] = "page 2"
              linkurl[1] = "page2.htm"

              which i am happy to do. but could it be even easier?
              a long time ago, i learnt bbc basic. this had a very useful feature: the DATA statement. imagine there is such thing as BBCBasicScript (don't tell me there is!!!), i would do this:

              numberoflinks = 10
              DATA page 1, page1.htm
              DATA page 2, page2.htm
              //etc.. (up to page 10 in this eg)

              see how much easier this would be to change later? the only number i have to change is the numberoflinks variable, not the array index. i don't even need quotes round strings.
              then to fill my arrays (this would never need changing):

              FOR fillarrays = 0 TO numberoflinks
              READ linktitle(fillarrays) // the READ statement gets the next DATA item, in order of declaration.
              READ linkurl(fillarrays)
              NEXT

              i know this is probably asking too much but does javascript have any kind of data storage feature? or am i stuck with explicitly declaring every variable?
              thanks for all the help, i have already gained a much better understanding of scripting.
              oh, and how is jscript different to javascript? why have they got (almost) the same name?
              neil.c

              Comment


              • #8
                Hi neil.c

                1. Javascript does not have file handling properties.

                2. Jscript is Microsoft's version of Javascript - it does have file handling properties but they only work in Internet Explorer (or the Windows scripting host) so they are not generally useful for the Internet (though very useful on an Intranet). If you want to play with these then you can find a full JScript reference on the MS site.

                3. You can actually read a .txt file - have a look at this script http://www.patrick-brennan.com/javas...Manalyzer.html - the source here isn't simply a .js file with a changed suffix - it's actually a GEDCOM file with a changed suffix.

                4. Whackaxe - I agree with your comments about PHP - it's my own favourite, but of course you need to have server-side access and I don't know whether neil.c has.

                Patrick
                Last edited by mpjbrennan; Jun 24, 2002, 11:53 AM.

                Comment


                • #9
                  well if he doesnt they he should make his own. the thought of setting up a server may sound daunting(it was for me) but if you get a nice user-friendly server like abyss from www.aprelium.com or another package from www.firepages.com.au then you should be set for scripting in php without hassle!
                  photoshop too expensive? use the GIMP! www.gimp.org

                  Comment


                  • #10
                    neil.c

                    you can do something like this in your external file...

                    link[0]=new Array{'linktitle', 'linkurl'};
                    link[1]=new Array{'linktitle', 'linkurl'};
                    link[2]=new Array{'linktitle', 'linkurl'};
                    link[3]=new Array{'linktitle', 'linkurl'};
                    link[4]=new Array{'linktitle', 'linkurl'};

                    then reference it in your code like..

                    link[x][0] is the link title
                    link[x][1] is the link url

                    I believe that should work.

                    Comment


                    • #11
                      thanks to everyone.
                      my server doesn't offer any server-side stuff, but like I said, I'm not particularly bothered about NS or IE3- being compatible.

                      I will use a .js file, like this:

                      var linkname = new Array()
                      var linkurl = new Array()
                      var i = 0
                      //the bit above stays as it is.

                      linkname[i] = "page 1"
                      linkurl[i] = "page1.htm"
                      i++
                      linkname[i] = "page 2"
                      linkurl[i] = "page2.htm"
                      i++

                      etc..

                      this way, i can easily add new pages without worrying about the array index number. using an explicit increment is a bit clumsy but it makes it easier to add and remove links. and i think it should be ok with other browsers anyway.

                      thanks again.
                      neil.c

                      Comment


                      • #12
                        Neil, you should really consider using a host (or hosting yourself) that can use a server-side language. You obviously have the understanding required.

                        Not to mention it will ease your troubles quite a bit.

                        Former ASP Forum Moderator - I'm back!

                        If you can teach yourself how to learn, you can learn anything. ;)

                        Comment

                        Working...
                        X