search

The Perfect Pop-Up

Key Takeaways

  • Pop-up windows can be used effectively and non-invasively, provided they are implemented correctly. Common faults include issues with scripting, search engines, accessibility, site management tools, pop-up killers, and options to stop pop-ups from opening.
  • To create a perfect pop-up, consider using a function that can be placed in some commonly-shared JavaScript code. This function can easily be called from anywhere on the site, and it is more preferable than writing the window.open function each time. The function should handle the focus aspect, assess the state of the pop-up, close the pop-up window, and re-open it with its new dimensions.
  • It is important to provide a link or button in the pop-up window itself to allow users to close it. However, if the user has scripting disabled, the ‘close this window’ link should be written to the Web page using JavaScript, and check if the window was opened as part of a window.open() method. If it is a true pop-up, the link will appear and the close() method will work; if it is not a true pop-up window, the link will not appear.

If you believe the likes of Jakob Neilsen and his supporters, nothing is more evil than pop-up windows. And in many ways, this is correct. Why? Well, we’ll list the reasons soon enough, but in a nutshell it’s because they are nearly always poorly implemented or simply not needed. This tutorial will show that, with the right thought, pop-up windows can be used without upsetting anyone – particularly the person browsing your site.

Problems With Pop-Ups

The common faults with pop-up windows are:

  • If scripting is disabled, or if the browser does not support JavaScript, the pop-up will not work
  • Search engines cannot follow links to pop-up windows (scripted elements are always ignored)
  • Pop-ups present accessibility problems
  • Site management tools (e.g. DreamWeaver) cannot update links to pop-ups if you move the destination page to another section of your site
  • Many people have pop-up killers running that close the window the moment it’s opened
  • In Mozilla, there is an option to stop pop-ups opening in the first place

Phew. That’s quite a list … and you could probably add your own to this list. So, how do we address these?

Scripting Disabled

With scripting disabled, the pop-up does nothing. Simple as that. But if you used a standard , there would be no such problem. So, instead of using:

<a href="#" onclick="window.open('file.htm');"> </a>

you might use:

<a href="file.htm" onclick="window.open('file.htm');return false;"></a>

This way, if scripting is disabled, the standard link still works.

However, perhaps there is a very good reason why you want the window to open on top of the current window. If so, just add a target attribute like so:

<a href="#" onclick="window.open('file.htm');"> </a>

Bingo. Problem solved. But there’s more we can do to this!

Search Engines

With the amended code above, search engines get a standard href to follow, so that’s another issue ticked off our problems list.

Accessibility Problems

The biggest fault with pop-ups is that they take the focus away from the main browser window, and this can be disconcerting. They also present general usability issues aside from accessibility. How often have you seen someone launch a pop-up and then inadvertently click back on the launcher window and, thinking that nothing’s happened, click the link again with no result? Of course the window has opened but it’s now under the launcher window, and only moving down to the task-bar and selecting the window from there will solve this.

The trick is to inform the user that the link will open in a new window. There are a number of ways to address this:

  • Include instructions as part of the link itself
  • Add some instruction in a title attribute
  • Use an appropriate icon to signify a pop-up is imminent

This way, if focus is lost, the user may make the connection, for example:

Open my test page (opens in a pop-up window)

The Perfect Pop-UpOpen my test page

To address the issue of losing focus on the main window, you can use JavaScript to re-set the focus. A proposed script for this appears at the end of this article.

If you’re in the habit of moving pages around using tools like DreamWeaver or a content management system, you would hope that links are maintained. With standard hrefs, they usually are (depending on the tool you use), but with JavaScript it’s unlikely. Returning to our code for a moment:

<a href="file.htm" onclick="window.open('file.htm');return false;"></a>

The link above would be maintained quite nicely … almost. Half of it would — the href part. But the onclick part would probably be ignored. This is a big problem. You might think that your links have been updated, but in fact people who do have JavaScript enabled would be sent to a missing page. So, you might find your code would be changed to:

<a href="file.htm" onclick="window.open('file.htm'); <br>
return false;" target="newWin"></a>

And if you were to run a link validator on the launch page, it would appear that your link is indeed valid. So, how do we address this issue? Like so:

<a href="file.htm" onclick="window.open('file.htm');  <br>
return false;"></a>

There’s only one link to maintain, and the correct href will be used for the window.open method. Excellent – now we’re getting somewhere!

Pop-up Killers/Mozilla Disable Pop-Ups

As with the issue of JavaScript being disabled, merely providing a standard href means that the link will still work. Now we only have to address the issue of which window has focus…

The Perfect Pop-Up Script

We recommend using a function that can be placed in some commonly-shared JavaScript code (as we’ve done with this site), and which can easily be called from anywhere in the site. This is far more preferable than laboriously writing the window.open function out each time. In addition to the URL, you might want to include parameters such as height and width, and what type of pop-up style to choose (it’s up to you what styles you define).

Here’s the code I recommend:

<a href="#" onclick="window.open('file.htm');"> </a>

The additional code in the function handles the focus aspect. If you click a link that calls this function, then click back on the page such that the pop-up is hidden, and then click on another pop-up link, the code assesses the state of the pop-up, then closes the pop-up window and re-opens it with its new dimensions.

To call the function you would use the following code:

<a href="file.htm" onclick="window.open('file.htm');return false;"></a>

Or, to use some actual examples:

This is my pop-up link (console mode)
This is my pop-up (fixed mode)
This is my (elastic mode)

Note: The ‘return false’ part effectively cancels out the default action of the href, so it won’t open the pop-up and a normal HTML windows – it will open one or the other. Try the links above with and without JavaScript enabled to see for yourself.

What more could you ask for? Well… there is one final piece of icing on this cake.

Closing the Pop-Up

The Perfect Pop-UpOnce the pop-up is opened, we might rely on people to use the browser/operating system controls to close the newly opened window.

But people don’t always do this! So we should provide a link (or button, if you prefer) in the pop-up window itself to allow users to close it. However, let’s assume that our user has scripting disabled, and that the pop-up window was opened via the standard href route. The ‘close this window’ link that you so thoughtfully provided will prompt a not very friendly dialogue like this:

The Perfect Pop-Up

To get around this problem, you should write the close link to the Web page using JavaScript, and check to see if the window was opened as part of a window.open() method. That way, if it is a true pop-up, the link will appear and the close() method will work; if it is not a true pop-up window, the link will not appear.

Here’s the code to do this:

<a href="file.htm" onclick="window.open('file.htm'); <br>
return false;" target="newWin"></a>

Try the link again, and see for yourself:

This is my pop-up (fixed mode)

Conclusion

Hopefully this tutorial has demonstrated that pop-up links can be accessible, search-engine friendly and non-invasive. However, even if you follow all of this advice you should still ask yourself if you really need to open a new window.

One final point to note is that pop-ups should be something that people opt-in to use, so don’t use a window.onload or window.onunload event to force your pop-up window on the user. That always annoys the heck out of people… unless they wanted to buy an X10 camera or visit the ‘World’s Largest Online Casino’ but didn’t know it, that is!

The above is the detailed content of The Perfect Pop-Up. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function