Results 1 to 3 of 3

Thread: Create Own Functions In Javascript

  1. #1
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Create Own Functions In Javascript

    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

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    1) language has been depreciated use
    Code:
    <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
    Code:
    <!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.
    Code:
    <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
    Code:
    <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>
    Last edited by boogyman; 09-07-2007 at 04:14 PM.

  3. #3
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up Creating Functions in Javascript

    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •