Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   js array как сделать (https://javascript.ru/forum/misc/26587-js-array-kak-sdelat.html)

zlodey 14.03.2012 07:07

js array как сделать
 
Привет, подскажите пожалуйста, столкнулся с проблемой сделать массив?
Имеем массив с 5 линками, например http://site.com1, http://site.com2, http://site.com3, http://site.com4, http://site.com5,

Нужно сделать что бы при выполнение этого скрипта выполнялся window.location рандомно на одно из значения массива.
Спасибо.

T-sh 14.03.2012 07:51

function randLocation(){
var r = Math.floor(Math.random() * 4);
var arr = Array("http://site.com1","http://site.com2","http://site.com3","http://site.com4","http://site.com5");
window.location = arr[r];
}

devote 14.03.2012 07:54

<script>
var links = [
    'http://yandex.ru',
    'http://google.ru',
    'http://javascript.ru',
    'http://spb-piksel.ru',
    'http://mail.ru'
];
document.location = links[ Math.floor( Math.random() * links.length ) ];
</script>

nerv_ 14.03.2012 10:29

Цитата:

Сообщение от Maxmaxmахimus
devote,
красиво пишешь

Да. Я беру с него пример по стилю :)


Часовой пояс GMT +3, время: 19:55.