2016-07-30 1 views
0

나는 암호를 요구하는 사이트를 만들고 있지만 일단 암호 프롬프트가 나타나면 다시 돌아 가기가 너무 어렵습니다. 암호의 목적은 사용자가 페이지와 페이지 소스를 보지 못하게하는 것입니다.
여기에 내가 지금까지이 작업은 다음과 같습니다닫기 탭/취소를 누르면 리디렉션?

 do{ 
      $('html').hide(); //hiding html with jQuery 
      var pass = "password"; //setting password 
      var selection = prompt("Enter password", ""); //prompting for password 
      if(selection == "cancel"){ 
       window.location.href = "http://www.example.com/"; 
      }; //trying (unsuccesfully) to make the page redirect if user answers "cancel" 
     } while(selection!=pass); 

답변

0

당신은 break 문을 필요로

do{ 
    $('html').hide(); 
    var pass = "password"; 
    var selection = prompt("Enter password", ""); 
    if(selection === null) { 
    window.location.href = "http://www.example.com/"; 
    break; 
    }; 
} while(selection !== pass); 

If the user clicks the Cancel button, this function returns null.

그래서, https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt에서,이 경우 브라우저 리디렉션하지 않기 때문에 여전히 while 루프처럼 코드가 실행 중입니다.

+0

내 테스트에서 발생하지 않는 취소 버튼을 누를 때 리디렉션해야합니다. –

+0

답변을 업데이트했습니다. – hansottowirtz

+0

정말 고마워요. D –