Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Using a single external file with two different blocks of code

    I have 2 arrays I've placed in an external file called "quotes.js". It's your basic random quote generator thing.

    Code:
    quotes = new Array(2);
    speakers = new Array(2);
    quotes[0] = "Quote 1.";
    speakers[0] = "Who Said It 1";
    quotes[1] = "Quote 2.";
    speakers[1] = "Who Said It 2";
    
    //mark
    index = Math.floor(Math.random() * quotes.length);
    document.write("\n");
    document.write("\""+quotes[index] + "\"<br/>");
    document.write("<h3>\- "+speakers[index]+"</h3>");
    I call it into my page with this:
    Code:
    <script SRC="quotes.js" language="javascript"></script>
    That works fine.

    However, I'd like to move the code in quotes.js (from "//mark" down) to a separate file so I can use my two arrays in two different files.

    In one, I just want to display a random quote using this code:
    Code:
    index = Math.floor(Math.random() * quotes.length);
    document.write("\n");
    document.write("\""+quotes[index] + "\"<br/>");
    document.write("<h3>\- "+speakers[index]+"</h3>");
    But in another file, I want to use this code, which displays every quote in the array at once.
    Code:
    for (i=0;i<quotes.length;i++)
    {
    document.write(quotes[i] + "<br >");
    document.write(speakers[i] + "<br ><br>");
    }
    My problem is that I don't know how to reference the data in the external JS file with these two blocks of code. The goal is that I'll only have to edit the single quotes.js file to update both the random quote page and the quote list page. I could just make two JS files, but what fun is that?

    I tried to find a solution to this on Google, but unfortunately I didn't come up with anything. I'm not very familiar with Javascript, so it's pretty likely that there's a term for all of this that would've helped me immediately.

    I'm guessing that what I'm looking for is just a simple command that says "look at quotes.js".

    I appreciate any help you can give.
    Last edited by BevansDesign; 09-30-2008 at 02:26 AM.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Code:
    quotes = new Array(2);
    speakers = new Array(2);
    quotes[0] = "Quote 1.";
    speakers[0] = "Who Said It 1";
    quotes[1] = "Quote 2.";
    speakers[1] = "Who Said It 2";
    
    function printSingle(){
      index = Math.floor(Math.random() * quotes.length);
      document.write("\n");
      document.write("\""+quotes[index] + "\"<br/>");
      document.write("<h3>\- "+speakers[index]+"</h3>");
    }
    
    function printAll(){
      for (i=0;i<quotes.length;i++){
      document.write(quotes[i] + "<br >");
      document.write(speakers[i] + "<br ><br>");
      }
    }
    when /where ever you want one: printSingle().
    when /where ever you want many: printAll().

    you can put the functions anywhere.
    just make sure your data (quotes + speakers) comes before calling the function.
    Last edited by rnd me; 09-30-2008 at 02:39 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    BevansDesign (09-30-2008)

  • #3
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, cool. I knew it'd be something easy like that. I've been trying to learn this stuff for years, but the best I can do is modify existing code. I guess this is why I'm a designer, not a programmer.

    Thanks a bunch!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •