랜덤 URL 질문
본문
a.html 클릭시 랜덤으로 url이 이동했으면 하는데요.
예시 ) 1. 네이버 2. 구글 3. 다음
이런 식으로 랜덤으로 들어갈 수 있는 html 소스 좀 주실 수 있으실까요 ㅠㅠ
답변 3
아래 소스를 잘 활용해 보시죠.
<a href="a.html" onclick="goUrl();">랜덤이동테스트</a>
<script>
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function goUrl() {
event.preventDefault();
var arrs = ["naver.com", "google.co.kr", "daum.net"];
var domain = shuffle(arrs);
location.href = "https://"+domain[0];
}
</script>
<?php
$arrays=array('http://www.naver.com"', 'http://www.daum.net', 'https://www.google.co.kr');
$selected=array_rand($arrays);
?>
<a href="<?php echo $arrays[$selected]?>">a.html</a>
<script>
urlArr = new Array('//www.naver.com"', '//www.google.co.kr' , '//www.daum.net' );
nn = Math.floor( Math.random() * urlArr.length);
location.href = urlArr[nn];
</script>