Wednesday, December 21, 2005

Detecting the popup-blocker

It is often useful to be able to open a new window to present the page containing the intended content (e.g., a page containing the actual experiment as was done here) in a way that is not possible from the user openen browser window. For example, you may want to get rid of the menu and toolbar, and have the window span the entire screen. This is easily achieved using the window.open function. The problem is that most web surfers have a popup blocker activated. There's no way to circumvent this, but you would like to be able to detect wether the creation of the popup window succeeded. Then you can present a warning to the user, explain the problem, and instruct the user to click a link on the current page to try again (the popup blocker doesn't block popups that are the direct result of a click event by the user). In MSIE you can use the window.createPopup function to create a popup that won't be blocked (this type of popup is subject to the restriction that only one popup can be open at the same time), but this is not available in other browsers. Here's a simple example:
   <html>
   <head>
     <script>
       url = 'http://www.google.com'
       message  = "A popup-blocker was detected!"
       message += " Please click the link to try again."
       popupProp = "fullscreen=yes, toolbar=no"

       function openIt() {
           var w = window.open(url, '_blank', popupProp)
           if(!w) // popup was blocked
              alert(message)
       }
    </script>
   </head>
   <body onload="openIt()">
    <h1> 
      If the window doesn't open
      <a href=# onclick="openIt()">
        click here
      </a>
      to try again.
    </h1>
   </body>  
   </html>
If you move your mouse over the square below you can see the above script in action.
Move your mouse over this square. If the window doesn't open click here to try again.
It doesn't necessarily have to be a click event, it can also be a keydown event. For example if you first click the square below and press the 'a' key, a popup window will appear. This is achieved by setting onkeydown="if(event.keyCode==65) openIt()". (See this post for making this compatible with Firefox.)
If this square has focus and the 'a'-key is pressed, a popup will open without being blocked.

Saturday, November 26, 2005

Nonlinear minimization in javascript

For certain experiments in psychology, especially those with tasks in which parameters are changed in response to the behavior of the subject ('adaptive testing'), it is useful to be able to minimize a function. DHTML is very useful for implementing such tasks (and an extemely cheap alternative for expensive experimentation software such as E-Prime), and so it is useful to have a javascript algorithm for function minimization. I found a useful C algorithm that was straightforward enough to translate into JavaScript (click the title of this post to download). It is an implementation of the Hooke and Jeeves algorithm (with improvements; read this for a conceptual explanation and comparison to other algorithms). Good things of this algorithm are: light weight and simple code, reasonably robust, multidimensional minimization, no gradients required, and open source. This javascript implementation is easy to use as the example below demonstrates:
  <script type=text/javascript src="hooke.js"><script/>
  <script>
  function g(x, n) // x contains the parameters, n is the # of params
  {
      c = -Math.exp(-(4-x[0])*(4-x[0])) + (x[1]*x[1]); 
      return (c);
  } 

  var console = document.getElementById("console")
  var a = new Hooke(g);  // new minimizer object
  a.startpt = [5, 1.9];  // starting point
  a.setConsole(console); // give a div element for diagnostic output
  a.main();              // start searching
  var result = a.endpt;  // array containing the coordinates found
  <script>
  <div id=console></div>

console

The full documentation is provided in the source.

Sunday, November 20, 2005

The EZ-Diffusion Model

We—that is, Eric-Jan Wagenmakers, Han van der Maas and me—have recently submited a paper which introduces the "EZ diffusion model" for two-choice reaction times, based on the diffusion model by Ratcliff (1978). To make application of the model easier we made a simple JavaScript program. Here's a batch-processing version. You only have to paste a table with the means, variances and proportions correct, and click a button to compute the EZ-diffusion parameters. The values are nicely formatted in a table so that you can copy easily copy and paste them back into your data analysis program. The original program can be found on E-J's site.

Update: An updated version of the EZ diffusion model, EZ2, is now available which also estimates the starting point/bias parameter. See EZ2