Popup Killer Test
It's simple to test whether or not a browser is JavaScript
enabled and to do appropriate actions:
<script language="Javascript"><!--
document.write('Yes! JavaScript!');
// --></script>
<noscript>
Your browser can't do JavaScript.
</noscript>
That's all you needed to do, back in the days when things
were simpler. Now, with popup killers on so many surfer's
machines, JavaScript enabled browsers won't necessarily
launch your popups.
This can be frustrating if you use popups for help text,
product descriptions, feedback forms, or other things
deemed necessary for a complete appreciation of your site.
No more.
This article will show you how to check whether or not your
visitor's browser allows popup windows.
This method has not been tested against all popup killers.
So please, if you find it doesn't work for a certain
browser/popup killer combination, tell us about it. You'll
find contact links at the URL in the signature file of this
article.
Here is how it works:
Your web page instructs the browser to make a popup. The
code in this article demonstrates how to determine if it
did or if it didn't succeed.
The Basic Code
The first thing to do is to put the following three blocks
of JavaScript code into the HEAD area of your web page.
It's important that the three blocks of code remain
separate because at least one popup killer disables all
JavaScript that's in the code block that would otherwise
create a popup. So, with separate code blocks only one
section is disabled and we can still use the rest of the
code. It is also important that the three blocks of code
are on your page in the same order as presented here. This
will ensure that the first block of code is executed before
the rest.
<script language="Javascript"><!--
TestString = 'failed';
// --></script>
<script language="Javascript"><!--
function InitializeTestPopup() {
TestPopup = window.open('','','height=100,width=100');
TestString = TestPopup;
TestPopup.close();
}
// --></script>
<script language="Javascript"><!--
function Verdict() {
if(TestString == 'failed' || TestString == null)
{
alert('No popup was made.');
}
else
{
alert('Yes, browser does popups.');
}
}
// --></script>
The first code block simply assigns a value to the
variable: TestString
The second code block attempts to create a popup. If it
succeeds, the variable TestString is assigned a complex
number related to the popup's location in computer memory
(once the value is assigned, the popup is automatically
closed). If the popup does not succeed, the variable
TestString will either be unchanged or it will be changed
to null.
The third code block can be modified for certain actions
(or inactions) depending on whether or not the browser will
do popups:
-
Replace the
alert('No popup was made.');
line with what you want to happen when the browser
will not make popups. You might change the message
in the alert box (alert boxes should still be
enabled even with popup killers) or replace the
line with a document.write() to print a message on
your web page, for examples. If you prefer that
nothing gets done when popups are disabled, replace
the line with two slashes: //
-
Replace the
alert('Yes, browser does popups.');
line with what you want to happen when the browser
makes popups. You might replace the line with a
popup launching function, for example. If you
prefer that nothing gets done, replace the line
with two slashes: //
Initializing the Test
The InitializeTestPopup() should be run when the page has
loaded. (InitializeTestPopup() is in the second block of code
in the HEAD area.) To do that, add an onLoad attribute to
your BODY tag, like this:
<body onLoad="InitializeTestPopup()">
Using the Code
Here are two ways to use the code:
-
Automatic test completion.
If you want to conclude the test immediately and
notify the user of the results, you can put the
following somewhere in your page's BODY area.
<script language="Javascript"><!--
setTimeout('Verdict()',1000);
// --></script>
It is important that the browser has at least one
second to create and destroy the test popup before
you use the Verdict() function. Otherwise, you
might get a false negative because the browser
hasn't had time for the create/destroy cycle.
That's why setTimeout() is used, with a delay of
1000 milliseconds.
The Verdict() function can be left as is if you
simply want a test result message to be displayed.
Otherwise, if you want to launch a popup
automatically, for example, replace the
alert('No popup was made.');
and
alert('Yes, browser does popups.');
lines with your required action (or two slashes
for inaction).
-
Complete test only when a link is clicked.
If you have help or definition links that create
popups, you can do the test when the link is
clicked. For example, let's assume your link was:
<a href="javascript:HelpPopup('Navigation')">
Then you would first change the link to:
<a href="javascript:Verdict()">
Then you would replace the
alert('No popup was made.');
line in the Verdict() function with:
alert('Sorry, popups must be enabled.');
and the
alert('Yes, browser does popups.');
line with:
HelpPopup('Navigation');
There are thousands of different popup implementations on
the net. With the above code and consulting the examples,
you should be able to adapt your pages quite nicely.
Will Bontrager
©2002 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.