WebDevelopersNotes logo

home-icon Home / JavaScript / Learning JavaScript Functions – 2

Learning JavaScript Functions – 2

Learning JavaScript Functions - 2

Here is how you solve the problem of using the same function to open windows with different urls.
Take a good look at the code below:

function open_win(url_add)

The function has a parameter url_add inside the parenthesis. It occurs in the function once again at the place where we specify the URL for the new window, however, this time it’s not enclosed in quotes.
To call the open_win function, we pass an argument which is the url address of the document we plan to display in the new window.


When open_win is called, we pass the url as an argument. Note that the url is a string data type and has to be enclosed in quotes.
The function picks up the argument and puts it in the value of the url_add variable. The variable containing a URL address (welcome.html) and is used inside the function in place of the actual URL string.

<A HREF="javascript:void(0)" 
onclick="open_win('welcome.html')">Welcome message</A>

Click to view the result

So if we want to open 10 pop-up windows each displaying a different URL, we call the function 10 times, each time using with a different URL as argument. The beauty is that we don’t have to write separate functions for displaying pop-up windows with individual URL addresses; the same function is sufficient.

Spicing up a page

I’ve placed this function in the HTML head.

function change_back(colorval)

I can then call it from several event handler code, as

<A HREF="javascript:void(0)"
Background Color 1</A>

Each link below, passes a different color code to the function. Move your mouse over the links below to see the effect.

Click to view the result

Sponsored Links

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

Sponsored Links


How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]


In a month of launch, Apple had sold more than a million iPads. [more...]

We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy