DO NOT TRY THIS AT HOME : Rotteneggs.com text files and message bases are for INFORMATIONAL PURPOSES ONLY. DO NOT undertake any project based upon any information obtained from this or any other web site.We are not responsible for, nor do we assume any liability for, damages resulting from the use of any information on this site.
(80 votes) Published: Jan 11, 2008 1:00 p.m. In 3 Favorites Lists Viewed 253 times
Once again Before you read this egg remember that wherever it says 5cript or js replace with s c r i p t and j a v a s c r i p t respectively.
Here is part 2 of my js egg. This, in my opinion, is the more interesting part as it deals with more things you can see as well as more user interactive features. Using this egg requires knowledge of the most basic js presented in my other here
This egg will cover:
-Alerts
-prompts
-functions
-buttons
Alerts:
Alerts are useful if you want to tell the user of your website something, warn them of something, or flat out annoy them (by using lots and lots of alerts).
This is what an alert looks like:
This is how an alert is created:
<5cript type="text/js">
alert("YOUR TEXT HERE");
</5cript>
Depending on where you place your alert in your html code your alert can pop up at different times
If you place it inside the head tag (shown below) the alert will pop up before the page loads.
<head>
<5cript type="text/js">
alert("Sylar");
</5cript>
</head>
If you place it inside the body tag (shown below) the alert will pop up after the page loads. (As shown by the awesome picture behind the alert)
Prompts are a way to obtain information from a user. For things like for loops and if/elses (explained in my previous egg) prompts can serve as a useful tool for receiving values.
Prompts are stored into vars (also explained in my previous egg) and are created like this.
<5cript type="text/js">
var your_var_name=prompt(“what you want above the prompt”);
</5cript>
This is what a prompt looks like:
Say you wanted to ask what a member on RE’s name was and afterwards create an alert saying “Hello (member name)”. You could do this like so:
<5cript type="text/js">
var name=prompt(“What is your name on RE?”);
alert(“Hello ”+name);
</5cript>
This is what that would look like:
Functions:
Functions are multiple pieces of code assigned to one variable that executes all of the pieces of code inside of it when called upon. (If that makes sense.)
Here is the basic structure of a function
<5cript type="text/js">
function your_function_name(optional var)
{
alert(Hello)
}
</5cript>
All you need to do is choose a name and create a body of code for it to execute. Functions are useful because they allow you to reuse code without rewriting it.
Say I create this function:
function say_hello()
{
document.write(“Hello”);
}
Now rather than typing document.write(Hello); to print Hello, I can just place say_hello(); and Hello will be printed like so:
say_hello();
say_hello();
say_hello();
Will result in:
A function that takes a variable can be useful because it can very easily be used with many values:
function multiply_by_five(userNum)
{
var product= userNum*5;
document.write(product);
}
This takes the user inputted variable, multiplies it by 5 and prints the product.
So if multiply_by_five(4) is called 20 will be returned because it takes 4 and multiplies it by 5.
Buttons:
Buttons are a nice component in a website that have the ability to execute a function. This is very convenient because for example, if you want an alert or a prompt to come up when a user chooses, you can set a button to the task.
The basic structure of a button is:
<input type="button" value="What you want the button to say"
Jan 11, 2008 1:04 pm - onclick="the function you want the button to execute">
So lets create an example function and button to execute it:
function get_name()
{
var name=prompt(“What is your name”);
document.writ (name);
}
When you click the button
It prompts for your name
Then prints it
If you write any code, show it to me, I’ll give you tips and show you improvements to make your code better.If you are having trouble with anything feel free to pm me at anytime. Like I said in the previous egg, I will (eventually) be adding this to my RE site. I will most likely write another programming language egg in the near future.