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:
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:
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!
- We use links to send information to the function.
- 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):
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 <<