Go to: Aardvark download page.


Sending parameters to functions is one of the coolest things in JavaScript. If you learn how to do this well you will become a great scriptwriter and save a LOT of time.

Let's say that you have 45 links in a page. All these links open different pages in popup windows. It wouldn't be fun if you have to write 45 different scripts, would it?

What you do in a situation like this is to write one script, a "window machine". The only thing the script needs to know is the URL to the page that you want to pop up and the size of the window. The rest of the script is the same for all links and windows!

Let's see if we can solve this one!

First we start by declaring a function and we give it a name:

<script language="JavaScript">
function superWindow(theURL,xSize,ySize){

For the first time you now see some receiving variables in the function declaration! The three variables will receive parameters from the 45 links.

window.open(theURL,"yourpagename","height="+ySize+",width="+xSize+", toolbar=0,menubar=0,directories=0,scrollbars=0,left=0,top=0");

This is the code that opens the window. I will not go into details how the code works. The important thing is that you can find the three variables embedded in this code.

// -->

Now we only need one more thing, links! We make the links like this:

<a href="javascript:superWindow('http://www.adobe.com',600,500)">

This is an ordinary link, but instead of a normal URL (like http://www.mySite.com) we add some JavaScript stuff.

The first part "javascript:" tells the browser that this is no ordinary link.

superWindow('http://www.adobe.com',600,500) is the call to our function. Note that every link contains a URL (in this case http://www.adobe.com), a width value and a height value for the window.

Now you can have one or thousands of links using this window machine, your very cool function!

I know this can be a bit hard to understand but it's one of the most vital things in JavaScript so here is all the important stuff one more time:

  • We use links to send information to the function.
  • Links are made like this: javascript:superWindow('http://www.adobe.com',600,500) . (That is what you enter in the URL box of the link inspector in GoLive)
  • Note that the URL is a string and therefore it's surrounded by quotes.
  • Commas separate all parameters.
  • In the function declaration we now had three variables inside the brackets. These variables will be "filled" with the data sent from the link.
  • The order of the variables in the function declaration must be the same as the order of the variables in the calling link.
  • We now know that a small script can handle thousands of windows and links!

Here is the script:(for copy and paste purposes):

<script language="JavaScript">
function superWindow(theURL,xSize,ySize){
toolbar=0,menubar=0, directories=0,scrollbars=0,left=0,top=0");
// -->

If you like experimenting try this:

  • Change the script so that the window size is constant and the links only send the URL.
  • Try to modify the script and send values that will open different windows in different positions (hint, top and left).

Back to tutorial index <<