PDA

View Full Version : Create Own Functions In Javascript



susie123
09-07-2007, 02:00 PM
I am new to JS and have tried desperately to work this problem out myself but no joy. I attach a file with the following code:

<html>
<head>
<title> Create own function exercise - Try It Yourself!</title>
<script language="javascript">
function webLink(message)
{
alert(message);
}
</script>

</head>
<body>

<form name="form1">
Enter your name here -
<input type="text" name="Name" size="14" onClick="webLink('Enter your name')">
<p>
Enter your favorite website here - www.
<input type="text" name="www." size="30" onClick="webLink('Enter your favourite website address')">
<p>

<script language="javascript">

var welCome="Welcome " + webLink;
var webSite="form1.www.";

document.write("Welcome " + name + ". Please go to " + webSite);

</script>
</form>
</body>
</html>

All I need now is for the visitorīs name to appear in the browser screen after the word "Welcome" and for their favourite website address to appear in the sentence, "Please go to www....", making that a hyperlink as well.

The prompt windows and everything else is just how I want it.

Thank you very much in advance.

S

boogyman
09-07-2007, 04:06 PM
1) language has been depreciated use


<script type="text/javascript">
</script>

2) and you should also use the doctype 4.01 so the browser can correctly identify and render your code properly


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PAGE TITLE</title>
</head>


now as for your functions put this inside your <head></head> tag.


<script type="text/javascript">
function getFavorites() {
var name = prompt("What is your name?", "");
var site = prompt("What is your favorite website?", "");

document.write("<h1>Welcome" +name+ "</h1>");
document.write('<p>Your favorite website is <a href="' +site+ '">' +site+ '</a></p>');
}
</script>


and your body would look something like


<body>

<form action="void(0)" method="get">
<input type="button" onclick="getFavorites()" value="My Name">
<input type="button" onclick="getFavorites()" value="My Favorite Site">
</form>

</body>
</html>

susie123
09-11-2007, 09:14 AM
Dear Boogyman

Thank you so much, it works!! I just need to add the http:// into the following and it worked perfectly ie

<a href="http://' +site+ '">

Thank you again.

:D