How can I get this code to a separate .js file?

Discussion in 'JavaScript' started by DGMarkets, Feb 24, 2010.

  1. #1
    I found this cool code for a banner rotator:

    <script language="Javascript">
    <!--
    // original content taken from Nic's JavaScript Page with permission
    // lack of these three lines will result in copyright infringment
    // made by: Nic's JavaScript Page - http://www.javascript-page.com
    
    var currentdate = 0;
    var core = 0;
    
    function initArray() {
    
    this.length = initArray.arguments.length;
      for (var i = 0; i < this.length; i++) {
      this[i] = initArray.arguments[i];
      }
    }
    
    link = new initArray(
    "http://link1",
    "http://link2",
    "http://link3"
    );
    
    image = new initArray(
    "http://image1.gif",
    "http://image2.gif",
    "http://image3.gif"
    );
    
    text = new initArray(
    "Banner 1",
    "Banner 2",
    "Banner 3"
    );
    
    var currentdate = new Date();
    var core = currentdate.getSeconds() % image.length;
    var ranlink  = link[core];
    var ranimage = image[core];
    var rantext  = text[core];
    
    document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');
    
    //-->
    </SCRIPT>
    
    Code (markup):
    But instead of using it inline I'd like to place it into a .js file as a function so I can call it from many different places in my blog, so I won't have to manually change every page when I want to change my banners/links. I've poked around at it but I'm just not a JS head and I keep stubbing my toes.

    If this is fairly easy can someone please show me the way? The code is working, I just need to wrap it up into a reusable file.

    Thanks.
     
    DGMarkets, Feb 24, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Using this script you have to write this code in every page, because the script writes a code in the page. You can do it using DOM, but at that time you have to write a div element in the every page. So you have to write something in the every page!!! :)
     
    s_ruben, Feb 25, 2010 IP
  3. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Simple just take your code and save it to a new file eg.) code.js

    Remove the <script> and the </script> tag and then reference it like so in your head tag of your page(s)

    <script src="code.js" type="text/javascript"></script>
     
    FCM, Feb 25, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    @FCM

    And what do you think about this row of code:

    document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');

    where the <a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a> will be written in the document?
     
    s_ruben, Feb 25, 2010 IP
  5. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #5
    Well just paste the <script src="code.js"></script> into a section where you want it to display it should work... I gave you an example for it to display on the top of the page. It doesn't matter where you put the code and where you put the code is where it will display so if you put the code in the footer it will display in the footer.
     
    FCM, Feb 25, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    @FCM

    But the code has to be put in the every page, yes? And DGMarkets doesn't do it in the every page. :)
     
    s_ruben, Feb 25, 2010 IP
  7. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #7
    Every page that you want it to display on yes... If you don't want it to display then don't add it. It would be impossible to automatically add it to every page without using some form of content management system like Joomla
     
    FCM, Feb 25, 2010 IP
  8. Dangy

    Dangy Well-Known Member

    Messages:
    841
    Likes Received:
    25
    Best Answers:
    2
    Trophy Points:
    155
    #8
    create a .js file with the below code in it.

    <!--
    // original content taken from Nic's JavaScript Page with permission
    // lack of these three lines will result in copyright infringment
    // made by: Nic's JavaScript Page - http://www.javascript-page.com
    
    var currentdate = 0;
    var core = 0;
    
    function initArray() {
    
    this.length = initArray.arguments.length;
      for (var i = 0; i < this.length; i++) {
      this[i] = initArray.arguments[i];
      }
    }
    
    link = new initArray(
    "http://link1",
    "http://link2",
    "http://link3"
    );
    
    image = new initArray(
    "http://image1.gif",
    "http://image2.gif",
    "http://image3.gif"
    );
    
    text = new initArray(
    "Banner 1",
    "Banner 2",
    "Banner 3"
    );
    
    var currentdate = new Date();
    var core = currentdate.getSeconds() % image.length;
    var ranlink  = link[core];
    var ranimage = image[core];
    var rantext  = text[core];
    
    document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');
    
    Code (markup):

    then add (This code must be displayed on every page you want the javascript on!)

    
    <script type="text/javascript" src="your file name.js"></script> 
    
    Code (markup):
    above
    
    </head>
    
    Code (markup):
     
    Dangy, Feb 25, 2010 IP