JavaScript problem
Results 1 to 6 of 6

Thread: JavaScript problem

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    163

    JavaScript problem

    looking for some javascript that will grab the contens of a txt file and save them to a variable.

    this is for a website i want to create which loads html code into a layer to reduce overall loading times of my pages.

    so far using dreamweaver i have this code...

    ----The button---
    <b onClick="MM_setTextOfLayer('main','','this is the contact page')">Contact[/b]
    ---

    ---The Java produced by dw of which i understand 3/4---

    <script language="JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.0
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
    }

    function MM_setTextOfLayer(objName,x,newText) { //v3.0
    if ((obj=MM_findObj(objName))!=null) with (obj)
    if (navigator.appName=='Netscape') {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
    }
    //-->
    </script>

    ---

    basically instead of "this is the contact page" i want to load html code from "contact.txt"

    now all i need is a pointer on how to do this...
    the only thing i don't know how to do is to get the contents of the txt file. i can define variables etc myself.

    any help is welcomed. if you need me to clarify or rephrase my question please let me know.
    thanx.

  2. #2
    Senior Member
    Join Date
    Mar 2003
    Posts
    117
    As far as I know, Javascript cant do includes or read/write another file. this is what server-side includes are for I guess.

    SSI cant be done with JS, but it's not that hard using PHP or ASP.

    Someone correct me if im wrong, long time since I was doing this sort of stuff
    .sig - There never was a .sig?
    I own a Schneider EuroPC with MS-Dos 3.3 and it works.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    163
    is there any file extention it could load from?
    if you can make javascript files that can be read then can't i save my html code in that manner and retrive the data like that?

  4. #4
    Senior Member
    Join Date
    Mar 2003
    Posts
    117
    Well the only way I know of, wich isn't what you are looking for, is to include a script file, like this:
    <script language="Javascript" src="globalscripts.js"></script>.

    This way of including a script file is used for global scripts, and easy control of them.

    This kind of include *could* be used for what you want, but i would not recommend it, and you would have to script everything with something like document.write();

    As I said, it was a long time ago since i coded anything in JS so Im not really sure, but you should definitely check out Server Side Includes, check if your serverhost has PHP or ASP running, if they do, its a simple syntax.
    .sig - There never was a .sig?
    I own a Schneider EuroPC with MS-Dos 3.3 and it works.

  5. #5
    Junior Member
    Join Date
    Jan 2003
    Posts
    10

    Smile Try this with Active X

    You'll need to have Active X enabled in order to read the file though.
    All you'll have to do is change the name and/or location of the file called "your_file.txt" below.
    I hope this helps!

    // This creates a new Active X Object in order to read in a file.
    // Refer to the new variable as "file"

    file = new ActiveXObject("Scripting.FileSystemObject");

    // Sets the variable "f" to equal the new open file contents

    f = file.OpenTextFile("c:\\your_file.txt", 1);

    // This reads in a single line

    line = f.ReadLine();

  6. #6
    Junior Member
    Join Date
    May 2003
    Posts
    6
    You can find several examples that you could possibly expand upon at www.javascriptsource.com .

Posting Permissions

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