Easty
Easty

Reputation: 387

What is the best way to hold data for jquery

Some time ago I wrote some quick Jquery code to select and advert at random below

    $(function () {

    var theImages = [ '*****.jpg', '******.jpg', '*****.jpg' ];
    var theLinks = [ 'http://www.****.co.uk', 'http://www.****.co.uk', 'http://www.*****.co.uk' ]
    var theRandomNumber = Math.round(Math.random() * (theImages.length - 1));
    $("#ad1").attr('src','images/' + theImages[theRandomNumber]);
    $("a#adL1").attr('href', theLinks[theRandomNumber]);
    var theRandomNumber2 = Math.round(Math.random() * (theImages.length - 1));
    while (theRandomNumber == theRandomNumber2) {
            theRandomNumber2 = Math.round(Math.random() * (theImages.length - 1));
    }
    $("#ad2").attr('src','images/' + theImages[theRandomNumber2]);
    $("a#adL2").attr('href', theLinks[theRandomNumber2]);
    } );
    </script>

I now have this code on 29 HTML pages in a website. Every time a ad changes I have to change every page. Can someone give some advice on the best and quickest way to put parameters in to I only have to update one thing. Then all of the pages will read these parameters and know what to display.

Thanks Paul

Upvotes: 0

Views: 175

Answers (2)

Curtis
Curtis

Reputation: 103428

You should put the javascript into a javascript (.js) file and include this in each of your HTML files like so:

<head>
   <script src="/RandomAdvert.js" type="text/javascript"></script>
</head>

Upvotes: 2

MattP
MattP

Reputation: 2863

Put it in a file such as my_ads.js

Then in the header of each page use

<script src="/your_path/my_ads.js" type="text/javascript"></script>

Upvotes: 0

Related Questions