How To Create A JavaScript Close Link

By  on  

Whenever I create a small JavaScript popup window, I offer a "close" link. Since I'm responsible for the new window, I feel it's my job to help them easily get rid of it.

The XHTML

Create an anchor with the JavaScript "close" call in the "href" attribute:

Close Window

Do not use the onClick attribute of the anchor because not everyone clicks links.

Note: I've gotten out of the habit of using JavaScript popups. They're annoying and as smooth as steel wool. I recommend using a modal box via JavaScript. Most modal box scripts allow for smooth transitions and are just as easy to close as popup windows -- plus, they don't get blocked by popup blockers. I currently use MoodalBox for my projects.

Recent Features

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!