Every time you use window.open, God kills a kitten
I was booking some travel recently and couldn't select a date on one of the travel sites because they were using window.open, so my browser blocked it as a popup. They weren't trying to sell me anything I wasn't planning on buying anyway, they weren't trying to install malware, it was just a date picker. But I couldn't use it without modifying my browser's security settings (or switching to a less secure browser). It's 2008... window.open? Seriously?
So, for any of you who still use window.open to display a date picker, or a comment form (see Andrew's blog as an example of the right way to do this), or... well, anything else... and the only reason is because you asked someone for an alternative and were simply told, "duh, use a positioned div" - but not told how - hopefully this post will show you how easy a better alternative can be.
To display something that looks like a popup but really isn't, all you really need is:
- a div that hides until needed and has a higher z-index than the rest of the page
- code to show and hide that div, and position it where you want it to display
What's a z-index? If you use layers in your Notes client development, you're already familiar with this concept: think of your user interface as existing in three-dimensional space. The x axis is horizontal, the y axis is vertical, and the z axis measures the conceptual distance between the "front" and the "back" of what's available to the user. Unlike x and y, however, which are typically measured in pixels or some other unit associated with the available width of the user's screen, the z index is theoretically limitless, and is simply identified by an integer. So on a web page (or a Notes layer), anything with a higher z-index is "closer" to the user; anything with a lower z-index is "further" away, and therefore displays "behind" the higher elements. Mozilla.org has a great tutorial on this concept if you want to dig deeper, but for the moment, just remember that the element with the highest z-index displays on top of everything else.
Here is an example (also available for download) of using a "positioned div" to display additional content that you might otherwise launch in a separate window with window.open. To enable this, start out by creating a div somewhere in your page markup, give it an id and a class (in my example, I'm using "pseudoWindow" as the id and "layerwindow" as the class), and define some CSS for that class:
div.layerwindow {
background-color: #eeeeee;
border: 3px solid #abcdef;
display: none;
overflow: scroll;
padding: 3px;
position: absolute;
z-index: 2;
}
This causes that div to hide initially, as well as defining how it will look when it's no longer hidden.
Next, you'll need some JavaScript code for showing the div, positioning it, and hiding it again:
var LayerWindow = function(){
var getViewPort = function(){ var viewPortWidth; var viewPortHeight; var vYscroll;
if (window.innerWidth) {
viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight;
vYscroll = window.pageYOffset;
} else if (document.documentElement && document.documentElement.clientWidth) { viewPortWidth = document.documentElement.clientWidth;
viewPortHeight = document.documentElement.clientHeight;
vYscroll = document.documentElement.scrollTop;
} else {
var bodyTag = document.getElementsByTagName('body')[0];
viewPortWidth = bodyTag.clientWidth;
viewPortHeight = bodyTag.clientHeight;
vYscroll = document.body.scrollTop;
}
return {
w: viewPortWidth,
h: viewPortHeight,
yScroll: vYscroll
};
};
return {
open: function(id, options){
var vp = getViewPort();
if ( |