Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Logical Operators and Conditional Statements - Java Script

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #1

Shy Logical Operators and Conditional Statements - Java Script


Well, let's say you wanted to get somebody's name before they saw the page, and then write their name on your page right before their very eyes.......Well, you can do this using a javascript prompt. Here's the command:

prompt('Your Question', ' ');

This will bring up a window asking the question of your choosing, with a space for the viewer to answer. The second set of quotes allows you to enter a default answer. If you leave it blank, the viewer will just see an empty box ready to be typed in. This is usually done before the page loads, so that you can write the answer they give into your page. To view an example, click the link below. You will get a prompt for your name, and your name will be written on the page!

Example:

Now, for the script that made this work. Note how the prompt and if/else statements are in the HEAD section, while the actual writing of the name occurs in the BODY section.


<SCRIPT language="JavaScript">
<!--hide
var yourname= prompt('Please enter your name, so you can get a special greeting', ' ');

if ( (yourname==' ') || (yourname==null) )
{
yourname="Dude";
}

//-->
</SCRIPT>

</HEAD>

<BODY>

<SCRIPT language="JavaScript">
<!--hide
document.write("<CENTER><H1>Hello, " + yourname + " ! Welcome to My Page! <\/H1><\/CENTER>");

//-->
</SCRIPT>

</BODY>

The first thing that happens is that the variable yourname is assigned the value it receives from the user from the prompt. So the variable yourname will be a string of characters that makes up the person's name. The if/else statement assigns yourname a value of "Dude" if nothing is entered in the prompt by the user. It checks for " " and for null, and both are pretty much nothing. Now, in the BODY section, you again use the SCRIPT tags to set off the JavaScript from any HTML around it. You will also see a new command called document.write(" "); . This is what allows the JavaScript variable yourname to be written onto the HTML document. You are writing two strings plus your variable, yourname. The variable yourname is not in quotes because it is a value and not itself a string (it's value is a string). That's why we have the plus signs around it....It makes the browser write the first string plus the variable plus the second string. Now, notice the HTML tags are inside the strings! Since this is a javascript, the only way to write the HTML tags back to the page is by including them inside the quotes as part of the string. Also, you probably noticed the way the closing tags were written differently. (<\/H1>). The backslash is there as the javascript escape character. It allows you to write the forward slash without it being mistaken for a division sign! (Remeber / is division in JavaScript). Thus using the backslash followed by a forward slash ultimately gives us.......our single forward slash. Pretty nifty trick, isn't it?


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
15-09-2006 03:17 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Toungue Image Rollover Script Javascript ratedguy 0 55 14-07-2008 06:15 PM
Last Post: ratedguy
Rolleyes Opening a New Window in Java Script ivenms 7 894 22-09-2007 04:40 AM
Last Post: Lord_Daksh
  Free Sudoku Script ivenms 6 901 23-07-2007 06:15 AM
Last Post: ivenms
Shy Logical Operators and Conditional Statements ivenms 0 597 15-09-2006 03:14 AM
Last Post: ivenms
  Declaring Variables and Functions on Java Script ivenms 0 573 15-09-2006 03:13 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 07-09-2008, 06:58 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB