Go Back   Velocity Reviews > Newsgroups > HTML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

HTML - automatically choose one of many .js files

 
Thread Tools Search this Thread
Old 02-25-2004, 08:15 AM   #1
Default automatically choose one of many .js files


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




Johan
  Reply With Quote
Old 02-25-2004, 09:27 AM   #2
Owen Jacobson
 
Posts: n/a
Default Re: automatically choose one of many .js files

On Wed, 25 Feb 2004 09:15:32 +0100, Johan wrote:

> 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.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

  Reply With Quote
Old 02-25-2004, 02:00 PM   #3
John Smith
 
Posts: n/a
Default Re: automatically choose one of many .js files

"Owen Jacobson" <> schreef in bericht
newsan.2004.02.25.09.27.02.730120@lionsanctuary. net...
> On Wed, 25 Feb 2004 09:15:32 +0100, Johan wrote:
>
> > 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.
>
> --
> Some say the Wired doesn't have political borders like the real world,
> but there are far too many nonsense-spouting anarchists or idiots who
> think that pranks are a revolution.
>

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


  Reply With Quote
Old 02-25-2004, 02:48 PM   #4
Johan
 
Posts: n/a
Default Re: automatically choose one of many .js files

> 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


  Reply With Quote
Old 02-25-2004, 03:03 PM   #5
John Smith
 
Posts: n/a
Default Re: automatically choose one of many .js files


"Johan" <> schreef in bericht
news:c1icja$1ii5oe$...
> > 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
>
>

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.


  Reply With Quote
Old 02-25-2004, 03:45 PM   #6
Johan
 
Posts: n/a
Default Re: automatically choose one of many .js files

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



  Reply With Quote
Old 02-25-2004, 04:07 PM   #7
John Smith
 
Posts: n/a
Default Re: automatically choose one of many .js files


"Johan" <> schreef in bericht
news:c1ifv4$1iubda$...
> 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.


  Reply With Quote
Old 02-25-2004, 04:15 PM   #8
John Smith
 
Posts: n/a
Default Re: automatically choose one of many .js files


"Johan" <> schreef in bericht
news:c1hlja$1ii767$...
> 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


  Reply With Quote
Old 02-25-2004, 05:38 PM   #9
Johan
 
Posts: n/a
Default Re: automatically choose one of many .js files


> 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


  Reply With Quote
Old 02-26-2004, 06:48 AM   #10
Johan
 
Posts: n/a
Default Re: automatically choose one of many .js files

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

"Johan" <> wrote in message
news:c1hlja$1ii767$...
> 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
>
>



  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump