테스트 사이트 - 개발 중인 베타 버전입니다

랜덤 URL 질문

그노초보요 6년 전 조회 3,003

a.html 클릭시 랜덤으로 url이 이동했으면 하는데요.

 

예시 ) 1. 네이버 2. 구글 3. 다음

 

이런 식으로 랜덤으로 들어갈 수 있는 html 소스 좀 주실 수 있으실까요 ㅠㅠ

댓글을 작성하려면 로그인이 필요합니다.

답변 3개

6년 전

<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>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

6년 전

<?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>
 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

6년 전

아래 소스를 잘 활용해 보시죠.

<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>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인