![]() |
![]() |
|
|
||
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:
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:
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. By: Will Bontrager Copyright 2002 Bontrager Connection, LLC
| ||