Welcome to another JavaScript tutorial of mine..This is not used so much but still its cool to know how it works! Here you will see how the prompt works :)
Solution:-
This code goes in the <head> tags:-
<SCRIPT language="JavaScript">
var visitorname= prompt('Please enter your name, '');
if ( (visitorname==' ') || (visitorname=="") || (visitorname==null) )
{
visitorname="Man";
}
</SCRIPT>
And this in the <Body> tags:-
<SCRIPT language="JavaScript">
document.write("<center>Hello, " + visitorname + "!<\center>");
</SCRIPT>
Explanation:-
if ( (visitorname==' ') || (visitorname=="") || (visitorname==null) )
{
visitorname="Man";
}
Here we are taking a 'precaution' so if the user enters nothing as a name or enters a space or presses Cancel we will take his name as 'Man' so we will tell him 'Hello, Man' instead of nothing..A Preview:-


Conclusion:-
As Always Feedback is welcome and the full source is attached!!


Sign In
Create Account



Back to top









