In a previous post, I provided JavaScript to open a page in a new window. It may sound funny, but once the window is opened, what does the visitor do when they're done with it? Generally, they can click on the red X in the upper right corner to close the window, but I've seen a lot of sites, and likely you have too, that provide a button that visitors can click on to close the window. This code is really easy, so today's tip is really short, but here goes.
The code to add a button that closes the current window is shown below.
<button onclick="javascript:window.close()">Close</button>
Alternatively, if you want to do this with text, you can use the A element and place the JavaScript code in the href attribute, as shown in the following code.
<a href="javascript:window.close()">Close</a>
There is one thing that you should know about this code. If you use the close method on a window that was not opened using script, Internet Explorer displays a message asking visitors if they want to close the window. In Netscape Navigator and Mozilla, the code won't work at all, and the JavaScript console displays the message "Scripts may not close windows that were not opened by script." So this example works best in a page that you display in a popup window using script code similar to the code I provided in my earlier post, open a page in a new window, mentioned above.
That's it. No magic here; nothing terribly complicated. Just simple code to close the active window. To use this code, just copy it into your page as is. When you display the page in the browser, you will see a button. Just remember that depending on the browser you use, it may not function or may not function as you want it to.
Well, that's pretty much my post for the week. It's been busy. I've been reworking the FrontPage Server Extension pages on MSDN. Okay, so this has been in the works for some time, but we've had a few glitches. The glitches seem to be smoothed, so if all works out well, we should have updated content available within about four weeks.
I'm reviewing all the feedback comments that people have provided as I update each page. That said, I want to give a plug for everyone disposed to rating MSDN articles. I don't know how other teams view this data, but in Office, we take customer comments for articles very seriously. Unfortunately, we can't always do anything about them.
For example, if someone rates an article as a 1, the lowest score possible, and provides a comment like, "This sucks!" or something negative about Microsoft, well, I hate to say it, but I can't do anything with a comment like that. So if you often rate, or even if you don't but would consider doing it if you knew that someone actually looks at your comments, please put in comments that help us to know how to revise the content. Were the instructions bad? Where? Which ones? Was something unclear? What? Be as specific as you can without writing a book.
Oh, and one more thing, although I have on occasion been known to respond to people who provide email addresses in article comments, I don't make any promises that the response will either happen or will happen quickly, so if you need assistance with an article and need a response back, the best way to do this is to send us an email. You can do this from our Feedback page. Just click Talk to Us, and an email window opens where you can write to us. I receive every one of those messages, and although I don't encourage it as a support mechanism, if you've exhausted all of your other options or need specific help with an article, I want to hear from you.
Alternatively, feel free to send me a message through my blog. Again, I respond to every message, and if someone provides an email address in a blog comment, I will often contact them directly as well as through the blog.
Have a great weekend.