automatically choose one of many .js files

J

Johan

Hi!

My index.html calls a .js file (getcontent.js).

<script language="JavaScript" src="getcontent.js">
</script>

I now would like it to automatically call one of my ten .js files
(1getcontent.js, 2getcontent.js and so on), instead of always calling
getcontent.js. And to do this in either a random way, or depending on for
example what hour or minute it is when the file is run.

Is this possible?

Would be glad if someone could help!

Johan
 
O

Owen Jacobson

My index.html calls a .js file (getcontent.js).

<script language="JavaScript" src="getcontent.js">
</script>

I now would like it to automatically call one of my ten .js files
(1getcontent.js, 2getcontent.js and so on), instead of always calling
getcontent.js. And to do this in either a random way, or...

Is this possible?

Yes. Have getcontent.js pull up the appropraite #getcontent.js file and
execute it. Implementing this is left as an excercise.
 
J

John Smith

Owen Jacobson said:
Yes. Have getcontent.js pull up the appropraite #getcontent.js file and
execute it. Implementing this is left as an excercise.
Perhaps you can add the last second you get with time in front of
getcontent.js
and then use document.write to load the file.
so if the seconds are 34 you will load 4.getcontent.js
and if the seconds are 56 you will load 6.getcontent.js
This way you'll pick the contant "at random"
How exactly this is done is the exercise as stated by Owen ;)
 
J

Johan

Perhaps you can add the last second you get with time in front of
getcontent.js
and then use document.write to load the file.
so if the seconds are 34 you will load 4.getcontent.js
and if the seconds are 56 you will load 6.getcontent.js
This way you'll pick the contant "at random"
How exactly this is done is the exercise as stated by Owen ;)

I thought about doing something like that. After a little more thinking, I
believe I don't need it to be random. I thought about taking the day of the
month (1,2,3...), or as you suggested the last second. But I might have more
"getcontents" in the future, more than 10, so it's probably better with day
of the month, or hour of the day. I suppose there is some "GetDate"
function, that returns 23 if it's the 23:rd?
I am already using document.write to load the file, further down in the
index.html. Looks like this:

<script language="JavaScript"><!--
var before = '<font size="4" <font color="#000000"> <b> <font
face="BernhardFashion BT">';
var after = '</font>';
var content = getcontent();
document.write(before + content + after);
// -->
</script>

I'm really new to all this, but should I really have to bother about this
later part (document.write part)? Shouldn't I just do what Owen told me, to
use getcontent.js to call the other #getcontent.js, and to put what you,
John, suggests (or something similar) in the .js that's named getcontent
(the first one, that calls the others)?
Thanks guys for pointing me in the right direction.

Johan
 
J

John Smith

Johan said:
I thought about doing something like that. After a little more thinking, I
believe I don't need it to be random. I thought about taking the day of the
month (1,2,3...), or as you suggested the last second. But I might have more
"getcontents" in the future, more than 10, so it's probably better with day
of the month, or hour of the day. I suppose there is some "GetDate"
function, that returns 23 if it's the 23:rd?
I am already using document.write to load the file, further down in the
index.html. Looks like this:

<script language="JavaScript"><!--
var before = '<font size="4" <font color="#000000"> <b> <font
face="BernhardFashion BT">';
var after = '</font>';
var content = getcontent();
document.write(before + content + after);
// -->
</script>

I'm really new to all this, but should I really have to bother about this
later part (document.write part)? Shouldn't I just do what Owen told me, to
use getcontent.js to call the other #getcontent.js, and to put what you,
John, suggests (or something similar) in the .js that's named getcontent
(the first one, that calls the others)?
Thanks guys for pointing me in the right direction.

Johan
the document.write part was a hint how to implement what owen told you
If you need more different contents you can pick any number of seconds and
with a simple mathematical calculation you can get any lower number of
possibilities.
an axample divide the last 100 seconds by 10 make it an Integer an you have
10 possibilities, divide it by 2 and ypu have 50 possibilities.
 
J

Johan

This might be a stupid question. Do I have to move the document.write part
from index.html to getcontent.js? I did split the original .js file into 10
separate ones in order to get the load time down, and to be able to call
parts of it separately. And if I have to put the document.write part in
every if - else loop in getcontent.js then there will be quite a lot of
code, and the load time won't be so much faster, am I right?
I was kind of hoping that Owens idea to call the #getcontents.js, would be
able to return, for example, 1getcontent.js back to the document.write part
in index.html. Isn't this possible?

Johan
 
J

John Smith

Johan said:
This might be a stupid question. Do I have to move the document.write part
from index.html to getcontent.js? I did split the original .js file into 10
separate ones in order to get the load time down, and to be able to call
parts of it separately. And if I have to put the document.write part in
every if - else loop in getcontent.js then there will be quite a lot of
code, and the load time won't be so much faster, am I right?
I was kind of hoping that Owens idea to call the #getcontents.js, would be
able to return, for example, 1getcontent.js back to the document.write part
in index.html. Isn't this possible?

Johan
The first javascript (might be part of the HTML file) decides which contents
should be loaded.
To have the browser load that .js file you have to write the script tag with
the created filename as HTML in javascript this is document.write("<script
src='xgetcontents.js'>") or something similar.
In xgetcontents.js there's data that needs to be displayed so this file wil
contain several times document.write().
Perhaps you should created a new OP and post it at a JavaScript newsgroup.
 
J

John Smith

Johan said:
Hi!

My index.html calls a .js file (getcontent.js).

<script language="JavaScript" src="getcontent.js">
</script>

I now would like it to automatically call one of my ten .js files
(1getcontent.js, 2getcontent.js and so on), instead of always calling
getcontent.js. And to do this in either a random way, or depending on for
example what hour or minute it is when the file is run.

Is this possible?

Would be glad if someone could help!

Johan
Perhaps this code will help you:

date = new Date();
now = date.getTime();
first = parseInt (now/10, 10);
second = parseInt (now/100, 10);
random = parseInt ((first-(second*10))/1.25,10);

contentfile=eval('<script language="javascript"
src="'+random+'getcontent.js" type="text/javascript"'></script>);
document.write(contentfile);

WARNING: The script tag might be depricated in some ways
 
J

Johan

Perhaps this code will help you:

date = new Date();
now = date.getTime();
first = parseInt (now/10, 10);
second = parseInt (now/100, 10);
random = parseInt ((first-(second*10))/1.25,10);

contentfile=eval('<script language="javascript"
src="'+random+'getcontent.js" type="text/javascript"'></script>);
document.write(contentfile);

WARNING: The script tag might be depricated in some ways

Thanks for trying to help, John! I havn't got it working, but I'll try more
tonight. I'll let you know tomorrow.
Once again, thanks!

Johan
 
J

Johan

I tried to implement your code for a few hours last night, but I didn't
manage. I don't think there's anything wrong with your code, but there's
some other problem. So, I guess I'll have to start thinking about doing it
in another way.
Is it possible to use if - else loops between the <head> tags? Something
like:

if randomnumber = 1
<script language="JavaScript" src="1getcontent.js">
</script>

if randomnumber =2
<script language="JavaScript" src="2getcontent.js">
</script>

If so, then it could be a solution.

Johan
 
J

John Smith

of course this is a solution but it can't be done in HTML it must be done
with javascript.
 
T

Toby A Inkster

Johan said:
if randomnumber = 1
<script language="JavaScript" src="1getcontent.js">
</script>

if randomnumber =2
<script language="JavaScript" src="2getcontent.js">
</script>

Johan, I really think you're approaching this the wrong way. Instead of
choosing a random number and then including different Javascript files,
why not just include the one Javascript file (getcontent.js) *always* and
then set that Javascript file to do something like:

-----------------------------------------------------------------
var randomnunber = some_function_to_make_a_random_number();

if (randomnumber == 1) {
// copy and paste the entire contents of
// 1getcontent.js here
}

if (randomnumber == 2) {
// copy and paste the entire contents of
// 2getcontent.js here
}

// etc
 
J

Johan

Okey, I'll check into that.
Thanks a lot, John, for taking the time to write the code, and for the
help you have provided!

Johan
 
J

Johan

The thing is, that index.html (including .js file) is to be loaded within a
few seconds (for modem users), and that the content of the page will be
changed, by the user, by him/her hitting F5 (it will be a lot of updating of
the page, several hundred updates of the page, so the load time must be as
low as possible). And the content of the getcontent.js grew pretty fast (and
the load time too), so that's why I split it into 10 separate .js files. I
also wanted the possibility to automatically switch between the different
..js files, either in a random way, or, preferably in an GetHour, or GetDate
sort of way (a different #getcontent.js file every hour, for example).
And I suppose that your solution, Toby, means that the hole .js file will
be read each time (even though it's only using part of its content), am I
right? If so, the load time would be too much. If not so, then it might be a
good solution. I'm hoping for the latter ;-)
I'm sorry that I didn't present all the information from the beginning.

Johan
 
T

Toby A Inkster

Johan said:
And I suppose that your solution, Toby, means that the hole .js file will
be read each time (even though it's only using part of its content), am I
right? If so, the load time would be too much. If not so, then it might be a
good solution. I'm hoping for the latter ;-)

Depends on the browser, cache settings, proxies, etc. IIRC (which I
probably don't!) Internet Explorer doesn't reload CSS and JS files when it
reloads a page.
 
J

Johan

But does IE read the hole .js file the first time a visitor access
index.html (which calls the .js file)?

Johan
 

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

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top