View Full Version : Creating a link using text from a prompt box
Millywoo
10-16-2002, 02:35 PM
I'm having trouble with an exercise where the task is to write a script which prompts the user for the URL address of their favourite web site, then display it on screen as a link.
I've written the following, but instead of the link destination being the web site, it's the name of the var i used, in this case, url:
function user()
{
var name = prompt("What's your name?","");
var site = prompt("What's the address of your favourite web site?","");
var url = "http://" + site ;
document.write("Welcome " + name + "<p>");
document.write("Click here to go to your favourite website: " + "<a href=>" + url + "</a>");
}
I then call the function in the body using onload.
Is there anyway of making the destination of the link correspond with the string from the prompt box?
Any help would be appreciated
Regards
:)
beetle
10-16-2002, 02:45 PM
document.write('Click here to go to your favourite website: <a href="'+url+'">' + url + '</a>');
Plus, you also may want to check if they already entered 'http://' before you automatically add it on.
landon11
10-16-2002, 02:47 PM
function user()
{
var name = prompt("What's your name?","");
var site = prompt("What's the address of your favourite web site?","");
var url = "http://" + site ;
document.write("Welcome " + name + "<p>");
document.write("Click <a href="+url+">here</a> to go to your favourite website.");
}
Millywoo
10-16-2002, 02:54 PM
Thanx! I'll work on a validation for it.
I noticed you used single quotes in document write, is that a user preference? I know about nesting and alternating double and single quotes, but wasn't sure if there was a standard for which came first.
beetle
10-16-2002, 03:20 PM
I prefer to use single quotes, and believe that it should pretty much be that way for this reason....future compatibility. XHTML requires (and HTML prefers) double quotes around it's attribute values. If you use single quotes for delimiting document.write statments, then you can just go a head and type in the doubles for the HTML, and escape any singles if you need to.
adios
10-16-2002, 06:32 PM
Just an observation: you should almost never call document.write() from an event handler, writing to the same window as the script is running in. This will, if the document (page) is completed, start a new one. Sometime it works, depending on what you're about, and sometimes it doesn't, but if you're just learning these techniques - I'd stay well away from it.
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
function getElement(id) {
return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) :
document.layers ? document.layers[id] : null;
}
function output(HTML)
{
var el = getElement('testlink');
if (el)
{
if (!document.layers && typeof el.innerHTML != 'undefined') el.innerHTML = HTML;
else
{
el.document.write(HTML);
el.document.close();
}
}
}
function user()
{
var url, result, name = prompt('What\\'s your name?','name');
if (!name || name == 'name') name = 'unknown loser'
url = prompt('What\\'s the address of your favourite web site?','http://');
if (!url || url == 'http://') url = 'http://www.codingforums.com';
if (url.indexOf('http://') == -1) url = 'http://' + url;
result = '<hr>Welcome, <b>' + name + '</b>.<hr>';
result += 'Click <a href="' + url + '">here</a> to go to your favourite website.';
output(result);
}
</script>
</head>
<body bgcolor="coral" onload="user()">
<div id="testlink" style="position:absolute;"></div>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.