How to randomly display advertisements such as link shares using JavaScript

How to randomly display advertisements such as link shares using JavaScript

Hello, this is naouniverse ( @naouniverse708 ).

As of the posting date and time of this article, an advertisement is displayed at the bottom of the sidebar.

One day, I came up with the idea of ​​using JavaScript to randomly display advertisements for things like link sharing, so I gave it a try.

<script type="text/javascript"> /* 数字は広告の数 */ var n = 3; var ad = new Array(n); /* 配列の各要素に任意のリンクタグを追加 */ ad[0] = 'リンクタグ1'; ad[1] = 'リンクタグ2'; ad[2] = 'リンクタグ3'; /* リンクタグ追加ここまで */ /* 乱数発生 */ var x = Math.floor( Math.random() * n ); /* 出力 */ document.write(ad[x]); </script>

Enclose the above link tag in single quotes "'".

If you surround the above link tag with double quotes "", you will need to add a backslash or a backslash before the "" in the link tag string, but this escape sequence (is it correct?) It's super troublesome...

Therefore, the link tag is enclosed in single quotes ''.

Also, it would be better if the image sizes of the advertisements are the same.

Although there is room for improvement, we were able to achieve the intended movement.
It also seems like it could be used in a variety of ways.

  • Add this entry to Hatena Bookmarks

At "naouniverse.com", the administrator writes about things, things, and the world that interest him.
Gadgets, cameras, design, programming, L'Arc~en~Ciel, etc...