O'Reilly logo

The Book of JavaScript, 2nd Edition by thau!

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required