Universal replace createPopup ()?

createPopup() currently only supported in IE (see http://help.dottoro.com/ljsxcrhv.php ).

Is there a universal replacement for createPopup() ? Or do you need conditional code based on browser discovery?

Hopefully I'm looking for something that not only provides the same functions, but has the same interface, or at least can provide the ingredients to create the createPopup() clone without much effort.

+7
javascript internet-explorer replace popup
source share
3 answers

You can look at some of the JavaScript libraries there. Things like Dojo, the Yahoo user interface, or JQuery can help encapsulate most browser-related headaches. For example, Dojo, see http://dojotoolkit.org/api/ . This will give you the same createPopup () functionality.

+1
source share

So, I had a whole mess of old code that used window.createPopup() , so switching to the library would be a big step, and now that IE 11 does not support this method, we had to do something with our application built for conductor support. I was able to solve this problem for working 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]; } 

I will start by recognizing that this is pretty ugly. However, it helped me make code that calls this method work in other browsers, and was easier than changing dozens of old (and poorly encoded otherwise) pages to use some external library, so maybe this will help someone something else.

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

+12
source share
0
source share

All Articles