OK Jane,
We're having fun here
You will need some more Javascript.
Your code is nearly correct.
You could code a function
function countryname(city)
{ if city == "New York" country = "USA"
if city == "London" country = "UK"
if city == "Paris" country = "France"
return country}
The only thing a little inefficient about this is that it keeps testing the
value of city even after a match has been found.
To avoid this, you can use "else"
function countryname(city)
{ if city == "New York" country = "USA"
else if city == "London" country = "UK"
else if city == "Paris" country = "France"
.............
else country = "Unknown"
return country}
Another way is the switch statement
function countryname(city)
switch (city)
{
case "New York" : country = "USA"; break
case "London" : country = "UK"; break
case "Paris": country = "France";break
....
default : country = "Unknown"
}
return country}
I like the other way of expressing the if else
variable = (condition1) ? 'value1' : 'value2'
In English this reads: If condition1 is true, then set variable to value1,
else set variable to value2.
The good thing about this is that instead of 'value2', you can add some more
conditions and values. The final value is the default value taken when all
others have been tested
e.g.
variable = (condition1) ? 'value1'
: (condition2) ? 'value2'
: (condition3) ? 'value3'
: 'defaultvalue"
So try this
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}
function countryname(cityname)
{var country = (cityname = 'New York') ? 'USA'
: (cityname = 'New York') ? 'UK'
: (cityname = 'Paris') ? 'France'
: 'Unknown'
return country}
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
var country = countryname(city) </script>
</head>
<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city + " country: " + country)
</script>
</body>
</html>
The result of this using the same test1.html was:
Hello JaneHere from New York country: USA
Of course, another embellishment is ask for the name and city in test1.html
and then pass the answers as parameters to test2.html.
That's another story. If you want to do it, I (or many others) can give you
the code.
--
Cheers,
Trevor L.
Website:
http://tandcl.homemail.com.au
Jane Here wrote:
> Trevor L,
> Yes, that works! Excellent.
>
> OK so now I want to go further and add some logic. Using a simple
> example, can you show me how to do it?
>
> Say I want to display the country based on the city.
> Something like:
> if city = "New%20York"
> country = "USA"
> if city = "London"
> country = "UK"
> if city = "Paris"
> country = "France"
>
> <body>
> <script type="text/javascript">
> document.write("Hello " + name + " from " + city + " country: " +
> country) </script>
> </body>
> </html>
>
> Jane