Often you need to create some way to display a message over the top of the current screen. This can be things like disclaimers or popup windows with ads in them etc. Instead of running into problems with using JavaScript window.open() events and Popup blockers you can use simple JavaScript and CSS properties to create an internal popup window or note infront of your HTML.
The possible uses of this are:
- Dynamic Notes or Messages, such as better presentation of Error Messages from Form Validation
- Popup Disclaimer over content that are hidden once the user hits “I agree”
- Displaying promotional material, such as a Flash advertisement
Typically using DHTML we are using JavaScript to change the properties of elements of a webpage. Each element appearing on a website has certain values which we can change dynamically based on events such as an onclick or hitting submit on a form.
Simple panel window over HTML content
The simplest example of a internal “popup” window that sits above the text. To do this we take advantage of the HTML element DIV. This element is a block-level element that by default does not contain any presentation attributes, like border, padding, margin or background. DIV’s have been widely accepted as the element to use when creating your HTML layout because of their flexibility and compliance with Content/Design seperation.
To create our simplest popup window we will apply some CSS-styles to the DIV element.
Z-Index (view w3c reference)
Z-Index terms to the layering of content. By default your HTML contents are placed at position 0. Imagine this as glass panes you look through on your computer screen. By assigning a element a Z-Index of 1 it creates another pane ontop of the original and it will sit cleanly over the elements at position 0.
Position (view w3c reference)
Position allows you to manage the location of where to place your popup window. You can use different attribute values like relative, absolute etc that allow you to position a DIV on your screen.
Visibility (view w3c reference)
The visibility property allows us to hide or display your HTML elements, in this case the DIV.
The CSS styles can be assigned into two classes that represent the states of your DIV; visible and hidden. Your DIV can then switch states by switching which style class is applied. We can do this by locating the element in the HTML document through the DOM (Document-Object-Model). To locate the object easiest we apply a unique ID to the DIV element (view w3c reference - Element identifiers: the id and class selector) and we can then use JavaScript to update the DIV elements class to represent its state.
Controlling a elements CSS property in JavaScript:
object = document.getElementById(’elementId’);
The JavaScript method ‘getElementById’ allows us to locate an element and gain a reference to it. Once we have a reference to this element we can access the properties of that attribute.
object.className = “cssClass”;
The property ‘className’ refers to the current classes applied to the element. It will start off containing the contents of the elements ‘class’ attribute. We can update this property to the name of the CSS class that represents the state of our DIV (eg. visible and hidden).
/* The positioning and presentation of our popup */
div.visibleState {
z-index: 1;
position: relative;
width: 250px;
height: 150px;
border: 1px solid #000;
background-color: #FFF;
}
/* The Element Hidden State */
.hiddenElement { visibility: hidden; display: none; }
Popup Notification Window
An alternative method to displaying messages from JavaScript would be to mix what we have learnt with controlling CSS states with JavaScript and introducing dynamic creation of the DIV contents. This way when we wish to display the a notification or alert, we can generate the HTML contents into a DIV and then switch the CSS property to represent its value.
Making use of the following JavaScript functions are useful in achieving popup notifications:
object.innerHTML = “Hello World”;
This allows us a fast way to quickly create HTML content into an element (instead of creating new objects and attaching VIA the DOM model - this method is faster as tested here). Once we locate our element (using getElementById) we can then change the contents by applying it to the innerHTML property of the object.
We could then create our own version of the javascript:alert(msg) function to display a nicely formatted popup notification.
function notifyMsg(id,msg) {
// Locate and update our popup window
object = document.getElementById(id);
object.innerHTML = msg; // We could also create some HTML to prepend our message
// Add our additional options such as OK
object.innerHTML += ‘whatever such as a close button who uses javascript to switch the state of this element to hidden’;
// Change the CSS state to visible
object.className = ‘visibleNotifyMsg’;
}
Usability of the Window
We have to consider certain affordance with the popup window, to ensure that the window is dealt with smoothly and intiuitively without causing a great hick-up to the user. Using a “X - Close” in the top right handle of your window will help create a quick way for people to close the message. Also a button at the bottom or image saying Close Window may prove useful as well.
You will also need to ensure that there is enough contrast created through the use of Borders or Background Colour that helps differentiate it from the content it is sitting upon. A white popup window with black text sitting over white background with black text will loose your user. If there is some sort of action required, consider the use of Colours to help your user identify the action required, eg. If poping up a Validation Alert, consider using Red Text or a Red Cross icon at the top of your message.
Click here to view the HTML Example file