Writing Flexible Functions

The code in Figure 6-3 does a good job of opening and centering a window containing No Starch Press's home page. But what if you wanted another link to open and center a different window with a different URL in it—Webmonkey's, for example?

One approach would be to write a second function that looks just like the first one, the only difference being that you'd replace the line

var the_window =
  window.open('http://www.nostarch.com/','the_window','height=200,width=200');

with the line

var the_window =
  window.open('http://www.webmonkey.com/','the_window','height=200,width=200');

This would work fine, but it's not a good idea to have two functions that do almost exactly the same thing. First of all, it's wasteful. If you could ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.