브라우저가 팝업을 차단하고 있는지 어떻게 알 수 있습니까?
때때로, 나는 사용자 입력 또는 중요한 것을 위해 새 창을 열려고 시도하는 웹 페이지를 보았지만 팝업 차단기는이 문제를 방지합니다.
호출 창이 새 창을 올바르게 시작하기 위해 사용할 수있는 방법은 무엇입니까?
JavaScript를 사용하여 팝업을 열면 다음과 같이 사용할 수 있습니다.
var newWin = window.open(url);
if(!newWin || newWin.closed || typeof newWin.closed=='undefined')
{
//POPUP BLOCKED
}
위의 많은 예제를 시도했지만 Chrome에서 작동하도록 할 수 없었습니다. 이 간단한 접근 방식은 Chrome 39, Firefox 34, Safari 5.1.7 및 IE 11에서 작동하는 것 같습니다. 다음은 JS 라이브러리의 코드입니다.
openPopUp: function(urlToOpen) {
var popup_window=window.open(urlToOpen,"myWindow","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=400, height=400");
try {
popup_window.focus();
} catch (e) {
alert("Pop-up Blocker is enabled! Please add this site to your exception list.");
}
}
업데이트 : 팝업은 실제로 고대부터 존재합니다. 초기 아이디어는 기본 창을 닫지 않고 다른 컨텐츠를 표시하는 것이 었습니다. 현재로서는 다른 방법이 있습니다. JavaScript는 서버에 대한 요청을 보낼 수 있으므로 팝업은 거의 사용되지 않습니다. 그러나 때로는 여전히 편리합니다.
과거에는 악의적 인 사이트가 팝업을 많이 남용했습니다. 나쁜 페이지는 광고로 많은 팝업 창을 열 수 있습니다. 따라서 대부분의 브라우저는 팝업을 차단하고 사용자를 보호하려고합니다.
대부분의 브라우저는 onclick과 같은 사용자 트리거 이벤트 핸들러 외부에서 호출되는 경우 팝업을 차단합니다.
생각하면 조금 까다 롭습니다. 코드가 직접 클릭 핸들러에 있으면 쉽게 할 수 있습니다. 그러나 setTimeout에서 팝업은 무엇입니까?
이 코드를 사용해보십시오 :
// open after 3 seconds
setTimeout(() => window.open('http://google.com'), 3000);
팝업은 Chrome에서 열리지 만 Firefox에서는 차단됩니다.
… 그리고 이것은 Firefox에서도 작동합니다.
// open after 1 seconds
setTimeout(() => window.open('http://google.com'), 1000);
차이점은 Firefox가 2000ms 이하의 시간 초과를 처리한다는 점이지만, 그 이후에는 "사용자 작업 외부"라고 가정하고 "신뢰"를 제거합니다. 따라서 첫 번째는 차단되고 두 번째는 차단되지 않습니다.
현재 2012 년의 원래 답변 :
팝업 차단기 검사를위한이 솔루션은 FF (v11), Safari (v6), Chrome (v23.0.127.95) 및 IE (v7 및 v9)에서 테스트되었습니다. 오류 메시지를 처리 할 수 있도록 displayError 함수를 업데이트하십시오.
var popupBlockerChecker = { check: function(popup_window){ var scope = this; if (popup_window) { if(/chrome/.test(navigator.userAgent.toLowerCase())){ setTimeout(function () { scope.is_popup_blocked(scope, popup_window); },200); }else{ popup_window.onload = function () { scope.is_popup_blocked(scope, popup_window); }; } } else { scope.displayError(); } }, is_popup_blocked: function(scope, popup_window){ if ((popup_window.innerHeight > 0)==false){ scope.displayError(); } }, displayError: function(){ alert("Popup Blocker is enabled! Please add this site to your exception list."); } };
용법:
var popup = window.open("http://www.google.ca", '_blank'); popupBlockerChecker.check(popup);
도움이 되었기를 바랍니다! :)
브라우저 회사 나 버전에 관계없이 항상 작동 하는 한 가지 "솔루션" 은 팝업을 생성하는 컨트롤과 가까운 곳에 화면에 경고 메시지를 표시하여 사용자에게 조치에 팝이 필요하다는 것을 공손하게 경고하는 것입니다. 사이트에서 사용하도록 설정하십시오.
I know it's not fancy or anything, but it can't get any simpler and only requires about 5 minutes testing, then you can move on to other nightmares.
Once the user has allowed pop-ups for your site, it would also be considerate if you don't overdo the pop-ups. The last thing you want to do is annoy your visitors.
I've tried lots of solutions, but the only one I could come up with that also worked with uBlock Origin, was by utilising a timeout to check the closed status of the popup.
function popup (url, width, height) {
const left = (window.screen.width / 2) - (width / 2)
const top = (window.screen.height / 2) - (height / 2)
let opener = window.open(url, '', `menubar=no, toolbar=no, status=no, resizable=yes, scrollbars=yes, width=${width},height=${height},top=${top},left=${left}`)
window.setTimeout(() => {
if (!opener || opener.closed || typeof opener.closed === 'undefined') {
console.log('Not allowed...') // Do something here.
}
}, 1000)
}
Obviously this is a hack; like all solutions to this problem.
You need to provide enough time in your setTimeout to account for the initial opening and closing, so it's never going to be thoroughly accurate. It will be a position of trial and error.
Add this to your list of attempts.
By using onbeforeunload event we can check as follows
function popup()
{
var chk=false;
var win1=window.open();
win1.onbeforeunload=()=>{
var win2=window.open();
win2.onbeforeunload=()=>{
chk=true;
};
win2.close();
};
win1.close();
return chk;
}
it will open 2 black windows in background
the function returns boolean value.
I combined @Kevin B and @DanielB's solutions.
This is much simpler.
var isPopupBlockerActivated = function(popupWindow) {
if (popupWindow) {
if (/chrome/.test(navigator.userAgent.toLowerCase())) {
try {
popupWindow.focus();
} catch (e) {
return true;
}
} else {
popupWindow.onload = function() {
return (popupWindow.innerHeight > 0) === false;
};
}
} else {
return true;
}
return false;
};
Usage:
var popup = window.open('https://www.google.com', '_blank');
if (isPopupBlockerActivated(popup)) {
// Do what you want.
}
참고URL : https://stackoverflow.com/questions/2914/how-can-i-detect-if-a-browser-is-blocking-a-popup
'Programing' 카테고리의 다른 글
C # 컴파일러로 생성 된 MSIL / CIL을 어떻게 볼 수 있습니까? (0) | 2020.07.17 |
---|---|
캐시 제어에서 개인 대 공개 (0) | 2020.07.17 |
git에서 체크 아웃을 취소하려면 어떻게합니까? (0) | 2020.07.17 |
여러 매개 변수를 전달하는 루비 전송 메소드 (0) | 2020.07.17 |
Atom 편집기에서 RegEx 구성 요소 검색 및 바꾸기 (0) | 2020.07.17 |