Javascriptでアフィリエイトバナーをランダムに表示する
目次
ページにアクセスするごとに、ランダムに選択されたアフィリエイトのバナーを表示するスクリプト。
Javascriptは初心者ですが、とりあえずは表示に成功。
「banner.js」を以下の内容でルートに作成。
(例:http://www.example.com/banner.js)
tag = new Array(); tag[0]='<a href="http://www.example.com/" target="_blank"><img src="http://www.example.com/banner1.jpg" alt="バナー1" border="0" /></a>'; tag[1]='<a href="http://www.example.com/" target="_blank"><img src="http://www.example.com/banner2.jpg" alt="バナー2" border="0" /></a>'; tag[2]='上記同様にバナーのタグを入れる'; i = Math.floor(Math.random()*tag.length); document.write(tag[i]);
HTMLファイルのバナーを表示したい箇所に以下のコードを記述。
(例えば、http://www.example.com/index.htmlの場合)
<script type="text/javascript" src="http://www.example.com/banner.js" charset="utf-8"></script>
こでだけでOK。
今後の課題は、一定時間ごとに表示したバナーをランダムに変更できるようにしたいと思います。
PHPで同じことを行う方法は以下を参照
http://qwerty.work/2009/05/php.html