JS -- Lesson 1

Lesson 1

This lesson will talk about prompts and alerts.

Prompts and alerts are both boxes that pop up.

Prompts pop up on entering a website usually, but can also be brought up when a function has a prompt (well go over functions in the next lesson). Prompts are used to get information from the person looking at your website, then you can use that information however you like. In this case i used the information you gave me and put it in the top left corner to tell you hi. Sometimes prompts dont work if you have pop up blockers or you are very secure, in order to see the prompt if you are secured, add this site to your trusted list.

Alerts pop up when you tell javascript to use an alert, and you can put text inside of it to alert people of something.

Let me show you some code:

var prompt_name = prompt("Hey please enter your name below:","Write your name here");

The code should be one line but it couldnt all fit, sometimes that will happen dont get too bent in shape about how to write it exact.

This bit of code as you see is a variable, and its name is prompt_name. This is not a special variable, its exactly the same as all the other variables, it is just a place to store information. The information we are storing is whatever the person writing in the prompt says. Then we have prompt("",""). Inbetween the first quotations is what you want people to enter in the box, it will be above the text box. Then the second quotations are what you put inside the text box, it isnt necessary and if you dont want anything in the text box just leave the space inbetween the 2 quotations empty.

Now whatever the person using the prompt puts in the text box will be stored in the variable "prompt_name". The retrieve this information you could just use document.write(prompt_name).

Now is the alert code:

alert("You clicked the button!");

Click the button below to see the result.

Kind of fun right?

This code says that we are going to make an alert box when you click the button.

As you can notice that isnt the complete code to make the alert/button combination. In HTML, is the button lesson, you need to learn that to understand the rest of the code. I will write the rest of the code for you.

<button onClick="alert('You clicked the button!');">Alert</button>

As you can see Javascript and HTML are used together sometimes, that is why it is important to learn both.

Lets go over the code:
This code must NOT be between the <SCRIPT LANGUAGE="Javascript> and </SCRIPT> tags or it will not count it as an HTML code, which it is HTML because of the tags. This code says we are creating a button, with the button tag and it has an attribute attached to it called an Event Handler. This specific event handler is onClick, which tells us when someone clicks the button something will happen. Put what you want to happen between the 2 quotes. We have an alert command in the quotes. Like the others it must end with a semi-colon to tell its command is done. To write what the button does do so between the button tags, then end the code with the ending tag for button. As you can see we have different quotes in the alert command, the ' and ' quotes. We use these because if we were to use the " and " quotes it would signify the end of the onClick attribute, because it uses whatever is inbetween its 2 quotes. We use the single quotes in between the double quotes to make sure it sees them as different parts of code. So to end, make sure to use single quotes when inside double quotes or it will mess you up.
Join Today
Join our Website's Jester Forum today!
Its FREE and easy......
Click Here


Enjoy the STAY

If you enjoyed this page,
add 2 favorites or tell a friend

Daily Event
Fortune of the Day

Quick Search
Ads & News
40199 visitors
=> Do you also want a homepage for free? Then click here! <=