Jump to content

JavaScript:Tutorial, Prompt

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Introduction:-
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:-
Posted Image

Posted Image

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

Attached Files



#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I'm just curious why you included all of this

(visitorname==' ') || (visitorname=="") || (visitorname==null)

I know in Java and PHP all three cases mean the same thing and only have to use one, is it necessary to include all three?

#3
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
I think it's because to make this work with all kinds of browsers. As some browsers ignore certain rules.