Accessing a variable in another file & file ops.

P

Peter

Hello,

Two newbie questions:

1) I have a javascript file with a function in it. From this function
I want to access a variable in another javascript file -which is not
inside a function. I have tried many combinations to do this, but has
yet to try the right one. How is it done?

2) What kinds of file operations can be done in Javascript (save,
read, write)? As far as I know all file operations are possible using
Java within your Javascript. But can't it be done using Javascript
only? (I'm talking file operations on my own files on my ISP's
server).

Thanks
/Peter
 
R

Randell D.

Peter said:
Hello,

Two newbie questions:

1) I have a javascript file with a function in it. From this function
I want to access a variable in another javascript file -which is not
inside a function. I have tried many combinations to do this, but has
yet to try the right one. How is it done?

2) What kinds of file operations can be done in Javascript (save,
read, write)? As far as I know all file operations are possible using
Java within your Javascript. But can't it be done using Javascript
only? (I'm talking file operations on my own files on my ISP's
server).

Thanks
/Peter

I can't answer your first question (though I suggest either making the code
available via a hyperlink or if its not too huge, including it with your
post in summary form and I'm sure someone would more quickly answer your
question).

With regards to question 2 - Javascript does not permit, nor does it contain
any functionality for disk access of any sort (ie you cannot
read/write/copy/move/whatever a file). This also means that you cannot even
perform anything so simple as a 'dir' or 'ls -l' or whatever command to list
files in a directory...
 
L

Laurent Bugnion, GalaSoft

Hi,
Hello,

Two newbie questions:

1) I have a javascript file with a function in it. From this function
I want to access a variable in another javascript file -which is not
inside a function. I have tried many combinations to do this, but has
yet to try the right one. How is it done?

You must include your other JS file with

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"
SRC="yourOtherFile.js"></SCRIPT>

After that, all the variables defined in the other file are available to
the script. All files and partial scripts are loaded in the page, so
that it becomes, in the end, one whole. This can cause problems,
however, especially if 2 global variables have the same name.

2) What kinds of file operations can be done in Javascript (save,
read, write)? As far as I know all file operations are possible using
Java within your Javascript. But can't it be done using Javascript
only? (I'm talking file operations on my own files on my ISP's
server).

I accept that you're speaking about client-side JavaScript. There are so
many possible configurations that it's impossible to detail all of them.
If you're talking about client-side operations on server-side files,
your possibilities are almost zero. All files operations should be
realized on the server, where you can very well use server-side
JavaScript (for example on ASP). A client-side Java applet can list
files in a directory on the server it originates from (if the server
allows directory browsing) and also read a file (text or binary).
Examples at
http://www.galasoft-LB.ch/myjava/WebLoadFile/Demo/Demo.html

With the correct permissions set, client-side JavaScript can also use
ActiveX (on IE only) to perform local file operations (list, write,
read...). In Netscape, local file operations can be performed using Java
File objects, once again in a relaxed security environment. But it's not
what you need and I mention it only for memory.

Thanks
/Peter

Detail exactly what you're trying to do, and I'll tell you if it's possible.

HTH,

Laurent
 
P

Peter

Well, I have a .js file with a function in it which can build a html
page. Something like this:

function buildPage(showContent) { // see below for info on
*showContent*
var header = "<html><head><title>SomeTitle</title></head><body>";

var footer = "</body></html>";

var page = header + content + footer; // see below for info on
*content*

document.write(page);
}

The argument *showContent* contains the .js file to access. This .js
file contains a variable called *content* which could look something
like this:

var content = "<p>This is the content</p>";

How do I access this variable? It needs to be in another file as I
have multiple files with the variable *content* containing different
information.

Thanks in advance for any help.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top