[IE javascript to Chrome] A universal createPopup() replacement

A universal createPopup() replacement

Questions : A universal createPopup() replacement

Currently createPopup() is only supported in IE (See http://help.dottoro.com/ljsxcrhv.php).

Is there a universal createPopup() replacement? Or is conditional code required based on browser detection?

Hopefully, I am looking for something that not only provides the same functionality, but has the same interface or at least could provide the ingredients to create createPopup() clone without too much work.

 

Answers 1 : of A universal createPopup() replacement

So I had a whole mess of legacy code that used window.createPopup() so changing to a library would have been a lot of effort, and now that IE 11 doesn't support this method, we had to do something since our app is built to support Explorer. I was able to solve this to work in other browsers by writing the following code:

if(!window.createPopup){
    window.createPopup = function (){
        var popup = document.createElement("iframe"), //must be iframe because existing functions are being called like parent.func()
            isShown = false, popupClicked = false;
        popup.src = "about:blank";
        popup.style.position = "absolute";
        popup.style.border = "0px";
        popup.style.display = "none";
        popup.addEventListener("load", function(e){
            popup.document = (popup.contentWindow || popup.contentDocument);//this will allow us to set innerHTML in the old fashion.
            if(popup.document.document) popup.document = popup.document.document;
        });
        document.body.appendChild (popup);
        var hidepopup = function (event){
            if(isShown)
                setTimeout(function (){
                    if(!popupClicked){
                        popup.hide();
                    }
                    popupClicked = false;
                }, 150);//timeout will allow the click event to trigger inside the frame before closing.
        }

        popup.show = function (x, y, w, h, pElement){
            if(typeof(x) !== 'undefined'){
                var elPos = [0, 0];
                if(pElement) elPos = findPos(pElement);//maybe validate that this is a DOM node instead of just falsy
                elPos[0] += y, elPos[1] += x;

                if(isNaN(w)) w = popup.document.scrollWidth;
                if(isNaN(h)) h = popup.document.scrollHeight;
                if(elPos[0] + w > document.body.clientWidth) elPos[0] = document.body.clientWidth - w - 5;
                if(elPos[1] + h > document.body.clientHeight) elPos[1] = document.body.clientHeight - h - 5;

                popup.style.left = elPos[0] + "px";
                popup.style.top = elPos[1] + "px";
                popup.style.width = w + "px";
                popup.style.height = h + "px";
            }
            popup.style.display = "block";
            isShown = true;
        }

        popup.hide = function (){
            isShown = false;
            popup.style.display = "none";
        }

        window.addEventListener('click', hidepopup, true);
        window.addEventListener('blur', hidepopup, true);
        return popup;
    }
}
function findPos(obj, foundScrollLeft, foundScrollTop) {
    var curleft = 0;
    var curtop = 0;
    if(obj.offsetLeft) curleft += parseInt(obj.offsetLeft);
    if(obj.offsetTop) curtop += parseInt(obj.offsetTop);
    if(obj.scrollTop && obj.scrollTop > 0) {
        curtop -= parseInt(obj.scrollTop);
        foundScrollTop = true;
    }
    if(obj.scrollLeft && obj.scrollLeft > 0) {
        curleft -= parseInt(obj.scrollLeft);
        foundScrollLeft = true;
    }
    if(obj.offsetParent) {
        var pos = findPos(obj.offsetParent, foundScrollLeft, foundScrollTop);
        curleft += pos[0];
        curtop += pos[1];
    } else if(obj.ownerDocument) {
        var thewindow = obj.ownerDocument.defaultView;
        if(!thewindow && obj.ownerDocument.parentWindow)
            thewindow = obj.ownerDocument.parentWindow;
        if(thewindow) {
            if (!foundScrollTop && thewindow.scrollY && thewindow.scrollY > 0) curtop -= parseInt(thewindow.scrollY);
            if (!foundScrollLeft && thewindow.scrollX && thewindow.scrollX > 0) curleft -= parseInt(thewindow.scrollX);
            if(thewindow.frameElement) {
                var pos = findPos(thewindow.frameElement);
                curleft += pos[0];
                curtop += pos[1];
            }
        }
    }
    return [curleft,curtop];
}

 

if(!window.createPopup){ window.createPopup = function (){ var popup = document.createElement("iframe"), //must be iframe because existing functions are being called like parent.func() isShown = false, popupClicked = false; popup.src = "about:blank"; popup.style.position = "absolute"; popup.style.border = "0px"; popup.style.display = "none"; popup.addEventListener("load", function(e){ popup.document = (popup.contentWindow || popup.contentDocument);//this will allow us to set innerHTML in the old fashion. if(popup.document.document) popup.document = popup.document.document; }); document.body.appendChild (popup); var hidepopup = function (event){ if(isShown) setTimeout(function (){ if(!popupClicked){ popup.hide(); } popupClicked = false; }, 150);//timeout will allow the click event to trigger inside the frame before closing. } popup.show = function (x, y, w, h, pElement){ if(typeof(x) !== 'undefined'){ var elPos = [0, 0]; if(pElement) elPos = findPos(pElement);//maybe validate that this is a DOM node instead of just falsy elPos[0] += y, elPos[1] += x; if(isNaN(w)) w = popup.document.scrollWidth; if(isNaN(h)) h = popup.document.scrollHeight; if(elPos[0] + w > document.body.clientWidth) elPos[0] = document.body.clientWidth - w - 5; if(elPos[1] + h > document.body.clientHeight) elPos[1] = document.body.clientHeight - h - 5; popup.style.left = elPos[0] + "px"; popup.style.top = elPos[1] + "px"; popup.style.width = w + "px"; popup.style.height = h + "px"; } popup.style.display = "block"; isShown = true; } popup.hide = function (){ isShown = false; popup.style.display = "none"; } window.addEventListener('click', hidepopup, true); window.addEventListener('blur', hidepopup, true); return popup; } } function findPos(obj, foundScrollLeft, foundScrollTop) { var curleft = 0; var curtop = 0; if(obj.offsetLeft) curleft += parseInt(obj.offsetLeft); if(obj.offsetTop) curtop += parseInt(obj.offsetTop); if(obj.scrollTop && obj.scrollTop > 0) { curtop -= parseInt(obj.scrollTop); foundScrollTop = true; } if(obj.scrollLeft && obj.scrollLeft > 0) { curleft -= parseInt(obj.scrollLeft); foundScrollLeft = true; } if(obj.offsetParent) { var pos = findPos(obj.offsetParent, foundScrollLeft, foundScrollTop); curleft += pos[0]; curtop += pos[1]; } else if(obj.ownerDocument) { var thewindow = obj.ownerDocument.defaultView; if(!thewindow && obj.ownerDocument.parentWindow) thewindow = obj.ownerDocument.parentWindow; if(thewindow) { if (!foundScrollTop && thewindow.scrollY && thewindow.scrollY > 0) curtop -= parseInt(thewindow.scrollY); if (!foundScrollLeft && thewindow.scrollX && thewindow.scrollX > 0) curleft -= parseInt(thewindow.scrollX); if(thewindow.frameElement) { var pos = findPos(thewindow.frameElement); curleft += pos[0]; curtop += pos[1]; } } } return [curleft,curtop]; }

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

I'll start by admitting that it's pretty ugly. However, this worked for me to make the code that calls this method work in other browsers, and was easier than changing dozens of legacy (and poorly coded otherwise) pages to use some outside library, so perhaps it will help someone else out there.

It uses an iframe and creates a document property on it because we had a lot of code that was along the lines of popup.document.body.innerHTML = "<span onclick = 'parent.someFunction()'>";. Using the iframe instead of a div allows this to remain in it's junky state and still work.

Answers 2 : of A universal createPopup() replacement

You may want to look at some of the JavaScript libraries out there. Things like Dojo, Yahoo UI, or JQuery can help to encapsulate most of the browser-specific headaches. For example, with Dojo, take a look at http://dojotoolkit.org/api/. This would get you similar functionality to createPopup().

Answers 3 : of A universal createPopup() replacement

Whats up with window.open()?

http://www.w3schools.com/jsref/met_win_open.asp

[출처] https://www.anycodings.com/1questions/1647707/a-universal-createpopup-replacement

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
20 [nest.js] [NestJS] NestJS 구조 이해를 위한 필수 개념 정리 - Node.js/TypeScript/Express 비교 포함 file 졸리운_곰 2025.12.12 501
19 [node.js 개발] Apache Reverse Proxy 설정(아파치와 노드 연동) file 졸리운_곰 2024.03.17 300
18 [node.js 개발] PM2로 Node.js 앱 프로세스 배포하기 file 졸리운_곰 2024.03.16 405
17 [node.js 개발] PM2를 활용한 Node.js 무중단 서비스하기 file 졸리운_곰 2024.03.16 517
16 [node.js 응용] Next.js : Next.js14에 Mysql연결하기 졸리운_곰 2024.03.03 379
15 [node.js 응용] Node.js에서 다른 파일의 함수를 "include" 하는 방법 졸리운_곰 2024.02.28 428
14 [node.js 응용] NodeJS 에서 mqtt 사용하기 file 졸리운_곰 2024.02.23 399
13 [node.js 응용] Next.js 기본 개념정리 file 졸리운_곰 2024.02.23 432
12 [node.js 응용] ejs 사용설명서 file 졸리운_곰 2023.11.25 371
11 [node.js 응용] Build a Node.js Proxy Server in Under 10 minutes! file 졸리운_곰 2023.05.07 468
10 [node.js 응용] node - pm2로 node.js 프로세스 관리하기 - 기본 명령어, 실행하기 file 졸리운_곰 2023.04.25 407
9 [node.js 응용] Node.js | MySQL과 연동(mysql모듈) - CRUD 2/2 졸리운_곰 2023.03.31 231
8 [node.js 응용] Node.js | MySQL과 연동(mysql모듈) - CRUD 1/2 file 졸리운_곰 2023.03.31 484
7 [node.js 응용] PM2 - Node.js 프로세스 관리 도구 file 졸리운_곰 2021.12.10 410
6 [node.js][nodejs] [Linux] 리눅스 내 Node.js 및 NPM 최신 버전으로 유지하기 file 졸리운_곰 2021.10.11 484
5 [node.js][typescript] 5분 안에 보는 TypeScript file 졸리운_곰 2021.07.03 428
4 Getting started with RabbitMQ and Node.js file 졸리운_곰 2019.05.09 466
3 [Node.js + RabbitMQ] Node.js + socket.io + RabbitMQ 이용한 실시간 메시지 처리 file 졸리운_곰 2019.05.09 352
2 node.js 서버 장애시 자동 재시작 설정 [forever 사용] 졸리운_곰 2019.01.24 884
1 Express 앱용 프로세스 관리자 졸리운_곰 2018.10.16 603
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED