automatically choose one of many .js files

Discussion in 'HTML' started by Johan, Feb 25, 2004.

  1. Johan

    Johan Guest

    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, Feb 25, 2004
    #1
    1. Advertising

  2. 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.
    Owen Jacobson, Feb 25, 2004
    #2
    1. Advertising

  3. Johan

    John Smith Guest

    "Owen Jacobson" <> schreef in bericht
    news:p...
    > 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 ;)
    John Smith, Feb 25, 2004
    #3
  4. Johan

    Johan Guest

    > 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
    Johan, Feb 25, 2004
    #4
  5. Johan

    John Smith Guest

    "Johan" <> schreef in bericht
    news:c1icja$1ii5oe$-berlin.de...
    > > 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.
    John Smith, Feb 25, 2004
    #5
  6. Johan

    Johan Guest

    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
    Johan, Feb 25, 2004
    #6
  7. Johan

    John Smith Guest

    "Johan" <> schreef in bericht
    news:c1ifv4$1iubda$-berlin.de...
    > 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.
    John Smith, Feb 25, 2004
    #7
  8. Johan

    John Smith Guest

    "Johan" <> schreef in bericht
    news:c1hlja$1ii767$-berlin.de...
    > 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
    John Smith, Feb 25, 2004
    #8
  9. Johan

    Johan Guest


    > 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
    Johan, Feb 25, 2004
    #9
  10. Johan

    Johan Guest

    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$-berlin.de...
    > 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, Feb 26, 2004
    #10
  11. Johan

    John Smith Guest

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

    "Johan" <> schreef in bericht
    news:c1k4r2$1jdbdq$-berlin.de...
    > 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$-berlin.de...
    > > 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
    > >
    > >

    >
    >
    John Smith, Feb 26, 2004
    #11
  12. Johan wrote:

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

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Feb 26, 2004
    #12
  13. Johan

    Johan Guest

    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

    "John Smith" <> wrote in message
    news:c1k5cp$tic$1.nb.home.nl...
    > of course this is a solution but it can't be done in HTML it must be done
    > with javascript.
    >
    > "Johan" <> schreef in bericht
    > news:c1k4r2$1jdbdq$-berlin.de...
    > > 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$-berlin.de...
    > > > 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, Feb 26, 2004
    #13
  14. Johan

    Johan Guest

    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

    > 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
    > -----------------------------------------------------------------
    >
    > --
    > Toby A Inkster BSc (Hons) ARCS
    > Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    >
    Johan, Feb 26, 2004
    #14
  15. Johan wrote:

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

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Feb 26, 2004
    #15
  16. Johan

    Johan Guest

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

    Johan

    > 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.
    >
    > --
    > Toby A Inkster BSc (Hons) ARCS
    > Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    >
    Johan, Feb 27, 2004
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    2
    Views:
    398
  2. Replies:
    3
    Views:
    316
  3. jason
    Replies:
    6
    Views:
    166
  4. NotGiven
    Replies:
    6
    Views:
    138
    oeyvind toft
    Sep 5, 2004
  5. Evolution
    Replies:
    1
    Views:
    106
    Evolution
    Apr 15, 2011
Loading...

Share This Page