Discussion Locked

Server Side javascript.

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi, I basically want to make a program that will rotate advertisements randomly (picture and hyperlink) in all my web pages. For this, I need to load the advertisements into an array and basically select a random item from that array. To avoid having to add all the data into each and every web page, I'd want to load my array up from a file, on the server. So, my problem is: "How do I load up a Javascript array with data that is in a file? Thank you your help is greatly appreciated and valued. I have this for now, and it is not getting interpreted as server side because when I check the source code, the code (other than the tags) is there.

Content-type: text/html\n

x = new File("source.txt");
fileIsOpen = x.open("r");

if (fileIsOpen)
{
while (!x.eof())
{
for (var i = 0; i < 3; i++)
{
pic[i] = x.readln();
}
for (var i = 0; i < 3; i++)
{
linker[i] = x.readln(); }
for (var i = 0; i < 3; i++)
{
compnay[i] = x.readln();
}
for (var i = 0; i < 3; i++)
{
linkwords[i] = x.readln();
}
for (var i = 0; i < 3; i++) {
lastwords[i] = x.readln();
}
}
}

3 replies

by ndtien

Hi all
I'm work in web development for one year but I don't know what your problem is. But I hope if I could help you. I think your javascript depends on your web server. What is your web server which you are using (the <server> tag looks strange).
I've worked with server-side javascript on IIS.
Bye

by ndtien

Hi all
I'm work in web development for one year but I don't know what your problem is. But I hope if I could help you. I think your javascript depends on your web server. What is your web server which you are using (the <server> tag looks strange).
I've worked with server-side javascript on IIS.
Bye

by peter.ford

Firstly, you need to configure your web server to interpret the script in the "" tags - that's not universal or even the right syntax for server-side JavaScript in a lot of cases. There's no real standard for this and every web server does it differently (e.g. BroadVision does server-side JS in <% %> tags like Java server pages). Then you need to get your pages located in a place that the server expects to find them which means reading your web-server's docs to work that out. Finally you should be realistic about what you can achieve with server-side scripting.
All the server can do is generate something for the browser to interpret. There is not clever message passing or two-way communication possible - the server knows nothing about the client and vice-versa. So you can use server side scripting to generate flat HTML or client-side JavaScript, and you could therefore build a page which contains all of your data in a JavaScript function or variable declaration. The only advantage of that would be that you can store that data in a single file and use it in several places - that might be what you want. You won't reduce page download time or page execution time on the browser, 'cos it still gets the same content.
Your example is not going to do what you think - all it will do is load the arrays up on the server side. The client doesn't know anything about what variables the server has in its memory, so you can't then go and use (for example) lastwords[1] in your client-side script - it's not defined on the client. The only thing you could do on server side is:

...
for (var i = 0; i < 3; i++)
{
lastwords[i] = x.readln();
out.print('lastwords['+i+']='+lastwords[i]);
}
...

where the "out.print()" function is some server-side JS function which writes it's argument to the HTTP response the server is making (a bit like document.write() in client side JS)

This discussion is closed to new replies.