User blog:Jarod Pillagebane/JavaScript

'''Well, I'm a tic bored, so in this blogpost I'll be showing you lot a tic about JavaScript. '''

<!DOCTYPE html>

Click the button to display a confirm box.

Try it



function myFunction

{

var x;

var r=confirm("Which are you—male or female? Male is 'OK,' and Female is 'Cancel.'");

if (r==true)

{

x="You pressed Male!";

}

else

{

x="You pressed Female!";

}

document.getElementById("demo").innerHTML=x;

}

The code above (^) makes a confirm box, such as the "Are you sure you want to leave this page?" popup when you have unpublished edits and you, for example, click a bookmark or type in a different URL.

However, if you wanted to make an alert/pop-up box with a skipped line in between it, you would not just press enter—the "/n" code is most often used for that. For example:

<!DOCTYPE html>

Click the button to demonstrate line-breaks in a popup box.

Try it



function myFunction

{

alert("Hello!\nMy name is Jarod.");

}

(Duly note you must have JavaScript enabled to use these codes.)

<!DOCTYPE html>

So, moving on. Click the button to demonstrate this box. Now, what this does is simply use the name entered and inserts it where the "name" code is, a bit like the "" code we use However, this plugs in whatever you enter in the box and not your username by default. Here's an example.

Try it



function myFunction

{

var x;

var name=prompt("Please enter your name","Jarod Pillagebane");

if (name!=null)

{

x="Hello " + name + "! How are you today?";

document.getElementById("demo").innerHTML=x;

}

}

Next, we have the alert box. The alert box is often used by websites to, well, alert you of something (hence its name). An example would be when making a purchase on, say, Amazon. You better pay attention to these alert boxes, because in a situation like when one has just purchased something on an online site and they click back, it duplicates that action—meaning you just ordered the item twice! Imagine if you were buying a car online. ..

<!DOCTYPE html>

So, moving on. Click the button to demonstrate this box. Now, what this does is simply use the name entered and inserts it where the "name" code is, a bit like the "" code we use However, this plugs in whatever you enter in the box and not your username by default. Here's an example.

Try it



function myFunction

{

var x;

var name=prompt("Please enter your name","Jarod Pillagebane");

if (name!=null)

{

x="Hello " + name + "! How are you today?";

document.getElementById("demo").innerHTML=x;

}

}

Now onto the next example. You can use JavaScript to make timed popups after clicking a button. In this case, I made it 3 seconds.

<!DOCTYPE html>

Click the button, wait 3 seconds, then you will see an alert.

Try it



function myFunction

{

setTimeout(function{alert("Your system has been compromised! (Kidding.)")},3000);

}

Here is a bit more complicated of a code. This code relates to mouseover descriptions and such. For this I chose the Sun, Venus, and Mercury.

<!DOCTYPE html>



function writeText(txt)

{

document.getElementById("desc").innerHTML=txt;

}









Mouse over the sun and the planets and see the different descriptions.

I'll probably add more codes later. Daag, and. ..

<font face=Papyrus size=12px color=Black> HAPPY CODING!