Adam Howitt's Blog

Mar 17
2005

Popup alternative

I want to share a little trick Jeff and I have employed for a new client in this world of banned popups.  A popup used to mean a dialog for the user to present important information without them navigating off a specific page and allow them to return to what they were doing.  Without this useful tool the user experience gets broken up so with my javascript magic and Jeff's CSS skills we put together this code snippet.

There is an example on this page of the technique so you can see if you even like the approach.  The search box used to take up valuable screen space so I removed it and used this technique to move it to a link under the recent posts section as "Search Archives".  Click the link and you see a yellow popup form.

Assuming you thought the search box looked good I'll procede with the tutorial.  First you need to create a div for the information you would ordinarily show in a popup:

<div id="whyWeAsk" class="popperHid">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus suscipit mauris ut nunc. Phasellus volutpat quam a nisl. Etiam ante. Nunc odio lacus, fringilla nec, lacinia sit amet, cursus sed, lectus. Cras urna felis, gravida egestas, convallis vel, viverra nec, diam. Morbi consequat, tellus nec consectetuer rutrum, lectus diam pulvinar metus, sit amet sodales pede nisl ut nulla. Cras pretium lectus vitae mauris. Morbi lorem. Mauris pretium placerat tellus. Proin in metus. Pellentesque nec orci. Nunc velit mi, pharetra eget, suscipit non, suscipit id, sem. Nunc sit amet nulla a dolor pharetra hendrerit. Class aptent taciti sociosqu ad litora torquent.
<div id="closebutton"><input type="button" value="close this message" onClick="showWhy();"></div>
</div>

Without any other modifications this should display inline wherever you placed it on your page.  Next we want to style the content to look pretty and also to hide onload so we add two styles for the "popper" itself and then style the buttons too.  Either add the following styles to your site.css file or use the in page style tag:

<style>
.popperHid {
    display:none;
}
.popperShow {
    position: absolute;
    z-index: 99;
    padding: 10px;
    border: 1px solid #600;
    width: 385px;
    left: 46%;
    top: 360px;
    margin: 0 0 0 -200px;
    background: #ffc;
    display:block;
    font-size: 90%;
}
.popperShow #closebutton {
    display: block;
    text-align: center;
    clear: both;
    margin: .5em 0 0 0;
    }
.popperShow #closebutton input {
    font-size: 70%;
    }
</style> 

Refresh your page and the popper becomes invisible since we have set the display property to "none".  The alternate style we defined uses "display:block" and there is some other wizardry to position the popper in the middle of your page regardless of browser width. 

Finally we add the following JavaScript function we mentioned in the initial popper div block and an href tag which, when clicked will call the toggling JavaScript function.

<script language="JavaScript">
function showWhy() {
    document.getElementById("whyWeAsk").className == 'popperHid' ? document.getElementById("whyWeAsk").className = 'popperShow' : document.getElementById("whyWeAsk").className = 'popperHid';
    return false;
}
</script>
<a href='' onclick='return showWhy();'>(Why we ask)</a> 

Put it all together and you end up with a handy tool to give a user context sensitive help on the page without navigating away from their current page.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. We have started to use div's to replace alert boxes on form validation. The first time round the form, divs will display with error message if the user leaves the field in an incomplete/unacceptable manner.

    Then after clicking 'Save' we change the background of the div's to red and show a message informing the user to correctly fill out fields and then submit again.

  2. I think that I'd probably have a div that just toggles between display:none and display:block; and then have it below the link that triggers the show/hide of the div. But I can see how some folks wouldn't like the "disruption" to the layout by adding and removing the search form and a "window" type approach could work better.

    I like how the DIV is placed so that the text field is part of the tab order after the Search Archives link is clicked.

    A nice little addition might be to place the focus automatically on the text field so that the user can just start typing to initiate their search experience rather than having to move their mouse over to the search window.

    PS: In case it matters, Firefox for some reason leaves the Search Archives link out of the tab order, probably because of the empty HREF.

  3. I have trie this one, But in ie6 does not show properly. Can you suggest me any solution for ie6

[Add Comment]