IFRAME이 최상위 창을 리디렉션하지 못하게하는 방법
일부 웹 사이트에는 IFRAME
인클로저 를 "분리"하는 코드가 있습니다. 즉, 페이지 A
가 IFRAME
상위 페이지 내부에 로드되면 P
일부 Javascript A
가 외부 창을 로 리디렉션합니다 A
.
일반적으로이 Javascript는 다음과 같습니다.
<script type="text/javascript">
if (top.location.href != self.location.href)
top.location.href = self.location.href;
</script>
내 질문은 : 부모 페이지 P
의 작성자이면서 내부 페이지의 저자가 아닌 경우이 분류를 수행 A
하지 못하게하려면 어떻게 A
해야합니까?
추신 : 그것은 사이트 간 보안 위반이되어야하는 것처럼 보이지만 그렇지 않습니다.
onbeforeunload 속성을 사용하면 사용자가 페이지를 탐색할지 여부를 선택할 수 있습니다.
예 : https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload
HTML5에서는 샌드 박스 속성을 사용할 수 있습니다. 아래 Pankrat의 답변을 참조하십시오. http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/
HTML5에서는 iframe 샌드 박스 속성이 추가되었습니다. 글을 쓰는 시점에서 이것은 Chrome, Safari, Firefox 및 최신 버전의 IE 및 Opera에서 작동 하지만 원하는 것을 거의 수행합니다.
<iframe src="url" sandbox="allow-forms allow-scripts"></iframe>
최상위 리디렉션 을 허용 하려면을 지정하십시오 sandbox="allow-top-navigation"
.
sandbox = "..."를 사용합니다
- allow-forms는 양식 제출을 허용합니다
- 팝업 허용 팝업 허용
- 포인터 허용 잠금 포인터 잠금 허용
- allow-same-origin을 사용하면 문서의 출처를 유지할 수 있습니다
- 스크립트 허용은 JavaScript 실행을 허용하고 기능이 자동으로 트리거되도록합니다.
- 최상위 탐색 허용은 최상위 창을 탐색하여 문서가 프레임에서 벗어나도록합니다.
상단 탐색은 방지하려는 것이므로 그대로두면 허용되지 않습니다. 빠진 것은 차단됩니다
전의.
<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="http://www.example.com"</iframe>
w3.org 스펙을 읽은 후 . 샌드 박스 속성을 찾았습니다.
을 설정 sandbox=""
하면 iframe이 리디렉션되지 않습니다. 그것은 iframe을 리디렉션하지 않는다고합니다. 본질적으로 클릭을 잃게됩니다.
예 : http://jsfiddle.net/ppkzS/1/
샌드 박스가없는 예 : http://jsfiddle.net/ppkzS/
나는 질문이 끝난 지 오랜 시간이 걸렸다는 것을 알고 있지만 여기에는 iframe 이로 드 될 때만 후속 호출을 위해 500ms를 기다리는 개선 된 버전이 있습니다.
<script type="text/javasript">
var prevent_bust = false ;
var from_loading_204 = false;
var frame_loading = false;
var prevent_bust_timer = 0;
var primer = true;
window.onbeforeunload = function(event) {
prevent_bust = !from_loading_204 && frame_loading;
if(from_loading_204)from_loading_204 = false;
if(prevent_bust){
prevent_bust_timer=500;
}
}
function frameLoad(){
if(!primer){
from_loading_204 = true;
window.top.location = '/?204';
prevent_bust = false;
frame_loading = true;
prevent_bust_timer=1000;
}else{
primer = false;
}
}
setInterval(function() {
if (prevent_bust_timer>0) {
if(prevent_bust){
from_loading_204 = true;
window.top.location = '/?204';
prevent_bust = false;
}else if(prevent_bust_timer == 1){
frame_loading = false;
prevent_bust = false;
from_loading_204 = false;
prevent_bust_timer == 0;
}
}
prevent_bust_timer--;
if(prevent_bust_timer==-100) {
prevent_bust_timer = 0;
}
}, 1);
</script>
and onload="frameLoad()"
and onreadystatechange="frameLoad();"
must be added to the frame or iframe.
I've found some useful hacks on this:
Using JS how can I stop child Iframes from redirecting or at least prompt users about the redirect
http://www.codinghorror.com/blog/2009/06/we-done-been-framed.html
http://coderrr.wordpress.com/2009/02/13/preventing-frame-busting-and-click-jacking-ui-redressing/
Since the page you load inside the iframe can execute the "break out" code with a setInterval, onbeforeunload might not be that practical, since it could flud the user with 'Are you sure you want to leave?' dialogs.
There is also the iframe security attribute which only works on IE & Opera
:(
In my case I want the user to visit the inner page so that server will see their ip as a visitor. If I use the php proxy technique I think that the inner page will see my server ip as a visitor so it is not good. The only solution I got so far is wilth onbeforeunload. Put this on your page:
<script type="text/javascript">
window.onbeforeunload = function () {
return "This will end your session";
}
</script>
This works both in firefox and ie, thats what I tested for. you will find versions using something like evt.return(whatever) crap... that doesn't work in firefox.
By doing so you'd be able to control any action of the framed page, which you cannot. Same-domain origin policy applies.
참고URL : https://stackoverflow.com/questions/369498/how-to-prevent-iframe-from-redirecting-top-level-window
'programing tip' 카테고리의 다른 글
Emacs를 사용하여 파일의 읽기 / 쓰기 모드를 어떻게 변경합니까? (0) | 2020.07.11 |
---|---|
파이썬에서 객체의 정규화 된 클래스 이름을 얻습니다. (0) | 2020.07.11 |
파이썬의 멀티 프로세싱 풀과 키보드 인터럽트 (0) | 2020.07.11 |
숨겨둔 내용으로 패치를 포맷하는 방법 (0) | 2020.07.11 |
세션 도용을 방지하는 가장 좋은 방법은 무엇입니까? (0) | 2020.07.11 |