랜덤 URL 질문

랜덤 URL 질문

QA

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

답변을 작성하시기 전에 로그인 해주세요.
전체 59,559
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT