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');">
you might use:
<a href="file.htm" onclick="window.open('file.htm');return false;">
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');">
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)
Open 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.
Site Link Management Tools
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;">
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">
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;">
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');">
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;">
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
Once 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:
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">
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.
