JavaScript Bookmarklet Advanced Programming by Christopher Topalian
JavaScript Bookmarklet Advanced Programming by Christopher Topalian
Dedicated
to
God the Father
"TECHNOLOGY BEYOND YOUR BELIEF STARK."
Copyright 2020-2021
All Rights Reserved
-
Hey Tony.
Tony?
They're on their way!
Table of Contents
Table of Contents
Where to Place Our Code.............................................................................................................11
Type the following Code in the Web Browser CONSOLE.................................................12
This is the Result of Our Code...................................................................................................13
Paste Code Into Web Browser Console..................................................................................14
This is the Result of Our Code...................................................................................................15
Web Browser Console or Bookmarks?...................................................................................17
Edit Our Bookmark to Add Javascript....................................................................................19
We Type Our Javascript Code in the Location field of our Bookmark.......................20
We Type this Code in the Location Field...............................................................................20
Activating Our Bookmarklet......................................................................................................21
Why do we make a Bookmark?...............................................................................................22
Paste Code Into the Bookmark.................................................................................................23
Adding Comments........................................................................................................................25
prompt, if statement, toLowerCase, alert, ==....................................................................27
if diagram.........................................................................................................................................28
if diagram with a test condition..............................................................................................29
Prompt, if else and toLowerCase ==.......................................................................................31
Prompt, if else and toLowerCase !=........................................................................................32
Prompt if else if, less than, greater than.............................................................................33
Prompt with if else if, do while loop......................................................................................35
do while loop diagram.................................................................................................................36
Prompt with if statement, while loop...................................................................................37
while loop diagram.......................................................................................................................38
Prompt, for loop............................................................................................................................39
for loop diagram...........................................................................................................................40
Prompt, for loop with break......................................................................................................41
VIDEO Pause...................................................................................................................................44
YouTube API VIDEO Pause.........................................................................................................46
VIDEO Play.......................................................................................................................................48
YouTube API VIDEO Play............................................................................................................49
VIDEO Back 2 Seconds..................................................................................................................51
VIDEO Forward 2 Seconds.........................................................................................................52
VIDEO Forward 2 Seconds – Keep Activating.....................................................................53
VIDEO currentTime.......................................................................................................................55
VIDEO currentTime with round...............................................................................................56
VIDEO currentTime with substring........................................................................................57
VIDEO currentTime to 60 seconds.........................................................................................58
VIDEO duration...............................................................................................................................59
VIDEO loop.......................................................................................................................................60
VIDEO muted....................................................................................................................................61
VIDEO unmuted.............................................................................................................................62
VIDEO currentSrc..........................................................................................................................63
VIDEO Volume – 0.0 Mute, 1.0 Full.........................................................................................64
VIDEO Volume – Get Volume Level........................................................................................65
VIDEO Speed to 0.5.......................................................................................................................66
VIDEO Speed Custom...................................................................................................................67
Object – Display Object Data with Alert...............................................................................69
Object – prompt.............................................................................................................................70
Object – prompt, if they don’t answer both..........................................................................71
Object – class constructor, method, return and createElement..................................73
Object – class inheritance..........................................................................................................75
New Window and HTML String................................................................................................79
New Window, HTML String, alert............................................................................................80
New Window, HTML String, alert,style..................................................................................81
New Window, HTML String, alert, style, js in the script tag..........................................83
New Window Tab, Go To URL, New Tab................................................................................86
New Window Go To URL in a New Window with Size......................................................87
New Window Go To URL in a New Window with Size and position............................88
New Window Go To Same URL as Current Window.........................................................89
createElement, append, Make a Paragraph........................................................................90
Youtube - GET URL, TITLE, DESCRIPTION, LIKES, DATE...................................................92
Create a Video Game Player on Any Webpage!.................................................................95
createElement, append, Make a div, style with JS..........................................................100
createElement, append, Make a div, style, url new window.......................................103
createElement, append, Make a div, style with css, url new window.....................105
createElement, append, div, css, url, window, array of objects.................................107
createElement, append, div, url, window, array of objects, show all........................110
createElement, append, make a Button...............................................................................113
createElement, append a Button with Style background, text color........................115
createElement, append, Button, Style, color, position, layer.......................................117
createElement and append, div...............................................................................................119
createElement, append, get link text, create div.............................................................120
createElement, append, get link text, button, click delete...........................................122
createElement, append, Button, Get URL of the Page you are on.............................124
createElement, append, Button, Style, color, pos, layer, VIDEO pause...................126
createElement, append, VIDEO, Play, Pause Buttons....................................................128
createElement Buttons VIDEO, play, pause, back, forward, Mute, UnMute..........130
createElement div, from object data....................................................................................137
createElement div, from object data, dot syntax of function.....................................139
createElement div, from object data, dot syntax of function......................................141
createElement div, class inheritance, dot syntax of function, pet............................143
Get Specific Button onclick value..........................................................................................147
Style All Paragraphs on Page.................................................................................................149
Number of Buttons on a Page................................................................................................150
Number of Links on a Page.......................................................................................................151
Show the InnerHTML of each Paragraph on a Page.......................................................152
Show the InnerHTML of each Paragraph with Style......................................................153
Show the href of each Link on Page.....................................................................................155
Show the href of each Link with Style.................................................................................156
Show All Image URLS.................................................................................................................157
Show the img tag with Style...................................................................................................158
Show the Buttons id’s on a Page...........................................................................................159
Show the Links id’s on a Page.................................................................................................160
Show the H1’s on a Page.............................................................................................................161
Show elements of a certain class on a Website..............................................................162
Inspecting an Element...............................................................................................................164
Timer – After 5 Seconds, Trigger function one time......................................................166
Timer – Every 5 Seconds, Trigger function........................................................................167
Timer – Every 1 Second, COUNT UP......................................................................................168
Timer – Every 1 Second, COUNT DOWN, clearInterval...................................................170
Timer – Every 1 Second, Trigger Military Time.................................................................172
Calculate DISTANCE....................................................................................................................175
Multiplied by the Time in Hours.............................................................................................175
Calculate SPEED...........................................................................................................................176
Calculate TIME..............................................................................................................................177
Calculate Storm Distance.........................................................................................................178
Calculate PERCENTAGE of MINUTES of an HOUR............................................................179
Calculate SALES TAX and TOTAL PRICE..............................................................................180
Calculate AVERAGE of 3 Numbers........................................................................................182
Calculate MEDIAN of TWO NUMBERS..................................................................................183
Calculate AREA of a SQUARE..................................................................................................184
Calculate PERIMETER of a SQUARE......................................................................................185
Convert FRACTION to DECIMAL..............................................................................................186
Convert FRACTION to PERCENT..............................................................................................187
Convert PERCENT to DECIMAL...............................................................................................188
Convert DECIMAL to PERCENT...............................................................................................189
Convert METERS to INCHES.....................................................................................................190
Convert INCHES to METERS......................................................................................................191
Convert METERS to FEET..........................................................................................................192
Convert FEET to METERS..........................................................................................................193
Convert FEET and INCHES to METERS.................................................................................194
Convert MILES to FEET..............................................................................................................195
Convert FEET to MILES..............................................................................................................196
Convert KILOMETERS to MILES..............................................................................................197
Convert MILES to KILOMETERS..............................................................................................198
Convert HOURS to MINUTES....................................................................................................199
Convert MINUTES to HOURS..................................................................................................200
Convert SECONDS to DAYS......................................................................................................201
Convert SECONDS to HOURS..................................................................................................202
Convert HOURS to SECONDS..................................................................................................203
Convert FAHRENHEIT to CELSIUS.........................................................................................204
return statement........................................................................................................................205
Console Log, Stringify All Records.......................................................................................206
Console Log, Stringify One Record......................................................................................208
Display One Record using innerHTML.................................................................................210
Display All Records, innerHTML.............................................................................................212
for loop ALL Records..................................................................................................................214
for loop ALL Records, Stringify..............................................................................................216
for loop ALL Records, Stringify, List.....................................................................................218
for loop ALL Records, Select Items, Spaces, New Line..................................................221
Filter for Year using if statement.........................................................................................224
for let in x, indexOf, Searching for Name...........................................................................227
Date and Time Simple...............................................................................................................230
Date Object - today's date and time......................................................................................231
Function Using return, show time........................................................................................233
Functions, return, Display Time to One Div......................................................................235
Get Day and Show Message....................................................................................................237
Get Month and Show Message..............................................................................................242
Get Hours and Show Message...............................................................................................247
Date and Time - Complete Time Code Script....................................................................249
We Have Only One Year Before the
Main Ship Arrives. When it does, it will
be total war on the entire planet!
We must prepare humanity. We need
an army of master programmers to
design a drone response.
Hey Tony, Nice to see you too buddy.
Where to Place Our Code
We Type our Code in the Console of the Web Browser
javascript: alert("Hi");
Hi Everyone
OK
javascript:(
function()
{
function ourFunction(name)
{
name = prompt("Enter Name");
return "Hi " + name;
}
alert(ourFunction());
})();
Enter Name
Chris Topalian
OK Cancel
Hi Chris Topalian
OK
JAVASCRIPT...LOADING...
JARVIS SYSTEMS
ONLINE...READY...
ADVANCED INTELLIGENCE
SYSTEMS ACTIVATED.
Web Browser Console or Bookmarks?
Bookmarklets VS Web Browser Console to
Activate our Code.
Activating Code in the Web Browser Console is very useful
for quickly and easily testing code by Copying and Pasting
from a Text File.
The next pages shows you step by step, how to put your
Javascript code in a Bookmark, but remember, you can keep
using the Web Browser Console method, if that is more
enjoyable for you. Happy Scripting!
How to Make Bookmarklets
We Start by Making a Bookmark,
which will contain our Javascript Code
in the Location Field.
Right Click on
the Bookmark
we made
Properties
Left Click on
Properties
Location
javascript: alert("Hi");
Hi
OK
javascript:(
function()
{
function ourFunction(name)
{
name = prompt("Enter Name");
return "Hi " + name;
}
alert(ourFunction());
} ) ( );
Select All of the Code in our Script Editor
and then press Ctrl + C to COPY.
Then Paste the Code in the Location Field
using Ctrl + V to PASTE.
A Message Box will appear that says Hi
and the person's name that they entered.
JAVASCRIPT IS THE MOST SPOKEN
LANGUAGE IN THE UNIVERSE STARK.
Why are you doing this Thanos?
Why are you helping us now?
Gamora!
Adding Comments
Comments are added using /* */
javascript:(
function()
{
function ourFunction(name)
{
name = prompt("Enter Name");
/* alert(name); */
return "Hi " + name;
}
alert(ourFunction());
} ) ( );
We do NOT use // to make comments!
if(name == "john")
{
alert("Hi " + name);
}
}
ourFunction();
} ) ( );
This alert message will trigger if the person types the
name John John john JoHN JOHN etc.
But, if the person uses spaces, it will NOT trigger.
Jo hn, or JOH N or J O H N, it will NOT trigger.
if diagram
start
test
FALSE
TRUE
body
Exit
if diagram with a test condition
if
FALSE
name == "john"
TRUE
Exit
Tony, you're asking me to teach
300 million people to become master
programmers in less than one year to
program drone armies to defeat who?
GALACTUS!
He plans on extermination!
Without the drone army, Earth Ends
in one year, when his ship arrives.
Prompt, if else and toLowerCase ==
== EQUAL TO
javascript:(
function()
{
function ourFunction(name)
{
name = prompt("Enter
Name").toLowerCase();
if(name == "john")
{
return "Hi John";
}
else
{
return "Hi " + name + " Where is
John";
}
}
alert(ourFunction());
} ) ( );
Prompt, if else and toLowerCase !=
!= NOT EQUAL TO
javascript:(
function()
{
function ourFunction(name)
{
name = prompt("Enter
Name").toLowerCase();
if(name != "john")
{
return "Hi " + name + " Where is
John";
}
else
{
return "Hi John";
}
}
alert(ourFunction());
} ) ( );
Prompt if else if, less than, greater than
< Less Than, > Greater Than
javascript:(
function()
{
function ourFunction(question) {
question = prompt("What is 5 x 5?");
if(question < 25)
{
return "A bit higher";
}
else if(question > 25)
{
return "A bit lower";
}
else
{
return "Correct";
}
}
alert(ourFunction());
} ) ( );
JJARVIS: SEARCH FOR HOW GALACTUS
LOCATED THE ANTI-LIFE EQUATION.
Jarvis simulation result:
"Darkseid"
THAT DOES NOT SOUND LIKE
A FRIENDLY FELLOW.
Prompt with if else if, do while loop
< LESS THAN > GREATER THAN != NOT EQUAL TO
javascript:(
function()
{
function ourFunction(question)
{
let message;
do
{
question = prompt("What is 5 x 5?");
if(question < 25) {
message = "A bit higher";
}
else if(question > 25) {
message = "A bit lower";
}
else {
message = "Correct";
}
alert(message);
} while(question!=25);
}
ourFunction();
} ) ( );
do while loop diagram
start
body
FALSE
Exit test
TRUE
Prompt with if statement, while
loop
< LESS THAN
javascript:(
function()
{
function ourFunction(players)
{
players = prompt("Enter Number of
Players");
let i = 0;
while(i < players)
{
document.write("Player " + i + ", ");
i++;
}
}
ourFunction();
} ) ( );
while loop diagram
start
FALSE
Exit test
TRUE
body
Prompt, for loop
We use a for loop to display how many
players the user chose
javascript:(
function()
{
function ourFunction(players)
{
players = prompt("Enter Number
of Players");
let i;
for(i=0; i < players; i++)
{
document.write("Player " + i +
", ");
}
}
ourFunction();
} ) ( );
for loop diagram
Start number
FALSE
Exit test
TRUE
body
increment++
Prompt, for loop with break
We use a for loop to display how many
players the user chose and use break if it is
not enough players chosen
javascript:(
function()
{
function ourFunction(players)
{
players = prompt("Enter Number
of Players");
let i;
javascript:(
function()
{
function ourFunction()
{
let theVideo =
document.querySelectorAll('video')
[0];
theVideo.pause();
}
ourFunction();
} ( ) );
javascript:(
function()
{
function ourFunction()
{
let theVideo =
document.getElementById("movie_pl
ayer");
theVideo.pauseVideo();
}
ourFunction();
} ( ) );
javascript:(
function()
{
function ourFunction()
{
let theVideo =
document.querySelectorAll('vide
o')[0];
theVideo.play();
}
ourFunction();
} ( ) );
YouTube API VIDEO Play
Plays ONLY YouTube Video using the
YouTube Api function
javascript:(
function()
{
function ourFunction()
{
let theVideo =
document.getElementById("movie
_player");
theVideo.playVideo();
}
ourFunction();
} ( ) );
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video').c
urrentTime += -2;
}
ourFunction();
} ( ) );
VIDEO Forward 2 Seconds
Skips the Video forward 2 seconds
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video').c
urrentTime += 2;
}
ourFunction();
} ( ) );
VIDEO Forward 2 Seconds – Keep
Activating
Skips the YouTube video Forward 2 seconds
repeatedly
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video')
.currentTime += 2;
}
setInterval(ourFunction, 1000);
} ( ) );
-
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video')
.currentTime;
alert(theTime);
}
ourFunction();
} ( ) );
VIDEO currentTime with round
Shows the rounded version of the
currentTime of the YouTube video
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video')
.currentTime;
alert(Math.round(theTime));
}
ourFunction();
} ( ) );
VIDEO currentTime with substring
Shows the shorter version of the currentTime
of the YouTube video
javascript:(
function()
{
function ourFunction()
{
let theTime =
document.querySelector('video').curr
entTime;
alert(theMessage);
}
ourFunction();
} ( ) );
VIDEO currentTime to 60 seconds
Sets the currentTime of the YouTube video
to 60 seconds (1:00)
javascript:(
function()
{
function ourFunction()
{
document.querySelector('video')
.currentTime = 60;
}
ourFunction();
} ( ) );
VIDEO duration
Shows the total length in seconds of the
YouTube video
javascript:(
function()
{
function ourFunction()
{
let theDuration =
document.querySelector('video')
.duration;
alert(theDuration);
}
ourFunction();
} ( ) );
VIDEO loop
Sets the Loop value to true of the
YouTube video.
javascript:(
function()
{
function ourFunction()
{
document.querySelector('video').loop
= true;
}
ourFunction();
} ( ) );
VIDEO muted
Mutes the volume of the YouTube video.
To un-mute the video, we can make
muted=false;
javascript:(
function()
{
function ourFunction()
{
document.querySelector('vide
o').muted = true;
}
ourFunction();
} ( ) );
VIDEO unmuted
Mutes the volume of the YouTube video.
To un-mute the video, we can make
muted=false;
javascript:(
function()
{
function ourFunction()
{
document.querySelector('vide
o').muted = false;
}
ourFunction();
} ( ) );
VIDEO currentSrc
Shows the blob URL of the YouTube video
javascript:(
function()
{
function ourFunction()
{
let theSrc =
document.querySelector('vide
o').currentSrc;
alert(theSrc);
}
ourFunction();
} ( ) );
VIDEO Volume – 0.0 Mute, 1.0 Full
Set the Volume of the YouTube video from
0.0 to 1.0
javascript:(
function()
{
function ourFunction()
{
document.querySelector('vid
eo').volume = 1.0;
}
ourFunction();
} ( ) );
VIDEO Volume – Get Volume Level
Gets the current Volume level of the YouTube
video. Volume level ranges from 0.0 to 1.0
javascript:(
function()
{
function ourFunction()
{
let theSrc =
document.querySelector('vide
o').volume;
alert(theSrc);
}
ourFunction();
} ( ) );
VIDEO Speed to 0.5
Set the speed value of the YouTube video
YouTube speeds are
0.0, 0.25, 0.5, 0.75, 1.00, 1.25, 1.5, 1.75, 2.00
But we can enter 20.0 if we like or 0.01
javascript:(
function()
{
function ourFunction()
{
document.querySelector('vid
eo').playbackRate = 0.5;
}
ourFunction();
} ( ) );
VIDEO Speed Custom
User Enters a Speed for the YouTube
Video to Play
javascript:(
function()
{
function ourFunction()
{
document.querySelector('vid
eo').playbackRate=
parseFloat(prompt('Enter a
Speed','0.50'));
}
ourFunction();
} ( ) );
THERE'S ONLY ONE YEAR BEFORE THE
MOTHERSHIP ARRIVES.
javascript:(
function()
{
let myPet =
{
name: "Tassey",
age: "20"
};
function ourFunction()
{
alert(myPet.name + " " +
myPet.age);
}
ourFunction();
} ) ( );
Object – prompt
User Enters the Data of an Object,
which is then displayed
javascript:(
function()
{
let myPet =
{
name: "Tassey the Turtle",
age: "20"
};
function ourFunction()
{
myPet.name = prompt("Enter Name");
myPet.age = prompt("Enter Age");
alert(myPet.name + " " + myPet.age);
}
ourFunction();
})();
Object – prompt, if they don’t
answer both
User Enters the Data of an Object,
which is then displayed, but if the User
doesn’t answer BOTH, a special message is
displayed
javascript:(
function()
{
let myPet =
{
name: "Tassey",
age: "20"
};
function ourFunction()
{
myPet.name = prompt("Enter
Name");
myPet.age = prompt("Enter
Age", "");
if (myPet.name == null ||
myPet.age ==null)
{
alert('Why not answer
both?');
}
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = david.greet() +
'<br>' + "Worker";
ourDiv.id = "theDiv";
document.body.append(ourDiv);
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.onclick= function(){ let
divRef =
document.getElementById("theDiv");
divRef.remove();};
document.body.append(ourDiv);
} ) ( );
Object – class inheritance
Class Inheritance allows us to make
instances of our class easily from a
blueprint, which we can add to anytime.
javascript:(function()
{
class Animal
{
constructor(kind, name, size,
weight, speed)
{
this.kind = kind;
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed + " mph";
}
eat()
{
console.log('eating');
return 'eating';
}
saySpeed()
{
console.log(this.speed);
return this.speed;
}
}
this.tailSize = tailSize;
}
meow()
{
console.log('meowing');
return 'meowing';
}
sayKind()
{
console.log(this.kind);
return this.kind;
}
tailWag()
{
console.log(this.tailSize + ' tail
wagging');
return this.tailSize + ' tail wagging';
}
}
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = felix.meow() +
'<br>' + felix.eat() + '<br>' + felix.say
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.onclick= function(){ let divRef =
document.getElementById("theDiv");
divRef.remove();};
document.body.append(ourDiv);
} ) ( );
New Window and HTML String
Make a New Window that has custom
content that we display as a string variable
javascript:(
function()
{
function ourFunction()
{
let html =
'<html><body><div>hi</div></body
></html>';
let ourWindow =
window.open("about:blank", "",
"_blank");
ourWindow.document.write(html);
}
ourFunction();
} ) ( );
New Window, HTML String, alert
Make a New Window that has custom
content that we display as a string variable
and that has functioning javascript
javascript:(
function()
{
function ourFunction()
{
let ourHTML = '<html><body>';
ourHTML += '<div
onclick=javascript:alert("Hi");>Click
Me</div>';
ourHTML +='</body></html>';
let ourWindow =
window.open("about:blank", "",
"_blank");
ourWindow.document.write(ourHTML);
}
ourFunction();
} ) ( );
New Window, HTML String,
alert,style
Make a New Window that has custom
content that we display as a string variable
and that has functioning javascript
javascript:(
function()
{
function ourFunction()
{
let ourHTML =
'<html><body><style>div{backgrou
nd-color:tan;}</style>';
ourHTML += '<div
onclick=javascript:alert("Hi");>Clic
k Me</div>';
ourHTML += '</body></html>';
let ourWindow =
window.open("about:blank", "",
"_blank");
ourWindow.document.write(ourHT
ML);
}
ourFunction();
} ) ( );
New Window, HTML String, alert,
style, js in the script tag
Make a New Window that has custom
content that we display as a string variable
and that has functioning javascript
javascript:(
function()
{
function ourFunction()
{
let ourHTML =
'<html><body><script>function
ourFunction(){alert("Howdy");}</scrip
t><style>div{background-
color:tan;}</style>';
ourHTML += '<div
onclick=ourFunction();>Click
Me</div>';
ourHTML += '</body></html>';
let ourWindow =
window.open("about:blank", "",
"_blank");
ourWindow.document.write(ourHTML)
;
}
ourFunction();
} ) ( );
New Window Tab, Go To URL, New
Tab
Opens a New Window to a specified URL
in a New Tab
javascript:(
function()
{
function ourFunction()
{
window.open('http://google.com/');
}
ourFunction();
} ) ( );
New Window Go To URL in a New
Window with Size
Opens a New Window with a specified URL,
height and width
javascript:(
function()
{
function ourFunction()
{
window.open('http://
www.google.com','targetname','height
=600,width=800');
}
ourFunction();
} ) ( );
New Window Go To URL in a New
Window with Size and position
Opens a New Window with a specified URL,
width and height and position on screen.
javascript:(
function()
{
function ourFunction()
{
window.open('http://www.goog
le.com' ,'targetname','height =
600,width = 800, left = 0px, top
= 0px');
}
ourFunction();
} ) ( );
New Window Go To Same URL as
Current Window
Opens a New Window with the URL of the
Current Page
javascript:(
function()
{
function ourFunction()
{
window.open(document.URL,'
_blank','location=yes,height=6
00,width=800,status=yes');
}
ourFunction();
} ) ( );
createElement, append, Make a
Paragraph
/* Create a New Paragraph on the page */
javascript:(
function()
{
function ourFunction()
{
let ourParagraph =
document.createElement("P");
ourParagraph.style.position =
"absolute";
ourParagraph.style.top = 100 +
'px';
ourParagraph.style.left = 100 +
'px';
ourParagraph.innerHTML = "Here
is the paragraph.";
document.body.append(ourParagr
aph);
}
ourFunction();
} ) ( );
Youtube - GET URL, TITLE,
DESCRIPTION, LIKES, DATE
javascript:(
function()
{
let nameList =
document.querySelectorAll(".style-
scope ytd-video-primary-info-
renderer");
let i;
let theNames = [ ];
for(i=0; i < nameList.length; i++)
{
theNames +=
nameList[i].textContent;
theNames += "\n";
}
//alert(theNames.toString());
let theTextArea =
document.createElement("textarea");
theTextArea.style.position="absolute"
;
theTextArea.style.left= 100 +'px';
theTextArea.style.fontFamily="exo";
theTextArea.style.fontWeight="normal
";
theTextArea.style.fontSize="medium";
theTextArea.style.width= 700 +'px';
theTextArea.style.height= 200 +'px';
theTextArea.style.background="white
";
theTextArea.style.textAlign="center";
theTextArea.setAttribute("readonly",
"true");
theTextArea.value = theNames;
document.body.append(theTextArea);
} ( ) );
Create a Video Game Player on Any
Webpage!
javascript:(
function()
{
/* i is slow, o is medium, p is fast speed
W is up, S is Down, A is Left, D is Right.
https://github.com/ChristopherTopalian/Topa
lian-Website-Game-Engine */
function ourFunction()
{
let makePlayer =
document.createElement("div");
makePlayer.style.position="absolute";
makePlayer.style.left=0;
makePlayer.style.top=0;
makePlayer.style.color="white";
makePlayer.style.zIndex="15";
makePlayer.style.fontFamily="exo";
makePlayer.style.fontWeight="bold";
makePlayer.style.fontSize="xx-large";
makePlayer.style.width="50px";
makePlayer.style.height="50px";
makePlayer.style.background="rgba(76,175,
180,0.5)";
makePlayer.style.textAlign="center";
makePlayer.id="thePlayer";
document.body.append(makePlayer);
playerId:document.getElementById("thePlay
er")
};
ourPlayer.playerId.style.background="rgba(0
, 175, 80, 0.5)";
ourPlayer.speedMultiplier=4;
}
if(e.keyCode==79)/*letter o */
{
ourPlayer.playerId.style.background="rgba(7
6, 0, 80, 0.5)";
ourPlayer.speedMultiplier=3;
}
if(e.keyCode == 73)/* letter i */
{
ourPlayer.playerId.style.background="rgba(7
6, 175, 180, 0.5)";
ourPlayer.speedMultiplier=2;
}
ourPlayer.playerId.style.left = ourPlayer.x
+ 'px';
ourPlayer.playerId.style.top = ourPlayer.y
+ 'px';
};
/* Player Controls */
let sensePlayerMotion = function(){
if(keyboard[keyboard.LEFT]){
movePlayer(-1,0);
}
if(keyboard[keyboard.RIGHT]){
movePlayer(1,0);
}
if(keyboard[keyboard.UP]){
movePlayer(0,-1);
}
if(keyboard[keyboard.DOWN]){
movePlayer(0,1);
}
};
/* Update the Position of the player */
movePlayer();
function scrollIt()
{
document.getElementById("thePlayer").scrol
lIntoView({block: "center", inline: "center"});
}
let theRefresh;
function doThisLoop()
{
sensePlayerMotion();
scrollIt();
theRefresh=requestAnimationFrame(doThis
Loop);
}
doThisLoop();
}
ourFunction();
} ) ( );
createElement, append, Make a div,
style with JS
Create a New div on the page and style
with JS methods
javascript:(function(){
function ourFunction()
{
let ourDiv =
document.createElement("div");
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px 2px";
ourDiv.style.background = "blue";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left = "100px";
ourDiv.style.zIndex="1000";
ourDiv.style.padding="10px";
ourDiv.innerHTML = "Copper" + "<br>";
ourDiv.innerHTML += "29";
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
createElement, append, Make a div,
style, url new tab
Create a New div on the page with style
and url link
javascript:(
function() {
function ourFunction()
{
let Elements =
{
name:"copper",
number:"29",
link:
"https://en.wikipedia.org/wiki/Copper"
};
let ourDiv =
document.createElement("div");
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left = "100px";
ourDiv.style.zIndex="1000";
ourDiv.style.padding="10px";
ourDiv.innerHTML = Elements.name +
"<br>";
ourDiv.innerHTML +=
Elements.number + "<br>";
ourDiv.innerHTML+='<a
href=\''+Elements.link+'\'
target="_blank">Link</a>';
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
createElement, append, Make a div,
style, url new window
Create a New div on the page with style
and url link
javascript:(function(){
function ourFunction(){
let Elements =
{
name:"copper",
number:"29",
link:
"https://en.wikipedia.org/wiki/Copper"
};
alert(Elements.link);
let ourDiv =
document.createElement("div");
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px";
*/
ourDiv.style.padding = "2px 2px
2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.zIndex="1000";
ourDiv.style.padding="10px";
ourDiv.innerHTML =
Elements.name + "<br>";
ourDiv.innerHTML +=
Elements.number + "<br>";
ourDiv.innerHTML += '<a
href=\'' +Elements.link + '\'
target="_blank">Link</a>';
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
createElement, append, Make a div,
style with css, url new window
Create a New div on the page with style
and url link
javascript:(function(){
function ourFunction()
{
let Elements =
{
name:"copper",
number:"29",
link:
"https://en.wikipedia.org/wiki/Copper"
};
let ourDiv =
document.createElement("div");
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px";
*/
ourDiv.style.padding = "2px 2px
2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.zIndex="1000";
ourDiv.style.padding="10px";
ourDiv.innerHTML =
Elements.name + "<br>";
ourDiv.innerHTML +=
Elements.number + "<br>";
ourDiv.innerHTML+='<a
href=\''+Elements.link+'\'
target="_blank">Link</a>';
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
createElement, append, div, css, url,
window, array of objects
Create a New div on the page with style and
url link of an array of objects
javascript:(function()
{
function ourFunction()
{
let myRecord = [
{
name: "Key",
weight: 140,
link: "https://google.com/news"
},
{
name: "Donald",
weight: 160,
link: "https://google.com"
}
];
let ourDiv =
document.createElement("div");
ourDiv.style.width = "100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.zIndex="1000";
ourDiv.style.padding="10px";
ourDiv.innerHTML =
myRecord[0].name + "<br>";
ourDiv.innerHTML +=
myRecord[0].weight + "<br>";
ourDiv.innerHTML+='<a
href=\''+myRecord[0].link+'\'
target="_blank">Link</a>';
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
We make an Array of Objects that we named
myRecord
The 0 entry of the myRecord array is Key.
javascript:(function()
{
function ourFunction()
{
let myRecord = [
{
name: "Key",
weight: 140,
link: "https://google.com/news"
},
{
name: "Donald",
weight: 160,
link: "https://google.com"
}
];
let ourDiv =
document.createElement("div");
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.position="absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.zIndex="1000";
document.body.append(ourDiv);
let output = '';
for(let i = 0; i < myRecord.length; i++)
{
output += myRecord[i].name + " ";
output += myRecord[i].weight + " ";
output +='<a
href=\''+myRecord[i].link+'\'
target="_blank">Link</a>';
output += '<br>';
}
ourDiv.innerHTML = output;
}
ourFunction();
} )( );
function ourFunction()
{
let ourButton =
document.createElement("BUTTON");
ourButton.style.position="absolute";
ourButton.style.top = "100px";
ourButton.style.left ="100px";
/* ourButton.style.width = "100px"; */
/* ourButton.style.height = "100px"; */
ourButton.style.padding = "2px 2px
2px 2px";
ourButton.style.background = "tan";
ourButton.style.color = "yellow";
ourButton.style.zIndex="1000";
ourButton.innerHTML = "This is a
Button.";
document.body.append(ourButton);
ourButton.onclick = function() {
textMessage() };
}
ourFunction();
} ) ( );
createElement, append a Button
with Style background, text color
Create a Button on the page with id, and alert
javascript:(
function()
{
function textMessage()
{
alert("hi");
}
function ourFunction()
{
let ourButton =
document.createElement("BUTTON");
ourButton.style.position =
"absolute";
ourButton.style.top = "100px";
ourButton.style.left = "100px";
ourButton.style.padding = "2px
2px 2px 2px";
ourButton.innerHTML = "This is
a Button.";
ourButton.id = "theButton";
ourButton.onclick = function() {
textMessage() };
ourButton.style.background =
"tan";
ourButton.style.color = "yellow";
ourButton.style.zIndex ="1000";
document.body.append(ourButton);
}
ourFunction();
} ) ( );
createElement, append, Button,
Style, color, position, layer
Create a Button on the page with an id,
alert, and style
javascript:(
function()
{
function ourFunction()
{
let ourButton =
document.createElement("BUTTON");
ourButton.id = "theButton";
ourButton.style.position =
"absolute";
ourButton.style.top = "100px";
ourButton.style.left = "100px";
/* ourButton.style.width = "100px"; */
/* ourButton.style.height = "100px"; */
ourButton.style.padding = "2px
2px 2px 2px";
ourButton.id = "theButton";
ourButton.onclick = function() {
alert("hi") };
ourButton.style.background =
"tan";
ourButton.style.color = "yellow";
ourButton.style.zIndex ="1000";
ourButton.innerHTML = "This is a
Button.";
document.body.append(ourButton);
}
ourFunction();
} ) ( );
createElement and append, div
Create a div on the page with id, and style
javascript:(
function()
{
function ourFunction() {
let ourDiv =
document.createElement("div");
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.innerHTML = "This is a div.";
document.body.append(ourDiv);
}
ourFunction();
} ) ( );
createElement, append, get link
text, create div
Create a div on the page with textContent of
each link found
javascript:(function()
{
function ourFunction()
{
let report;
let theLinks =
document.getElementsByTagName("a
");
let i;
for (i = 0; i < theLinks.length; i++)
{
report +=
theLinks[i].textContent;
report += "\n";
}
return report;
}
ourFunction();
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = ourFunction();
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px
2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.onclick= function() {
document.getElementById("theDiv").i
nnerHTML=""};
document.body.append(ourDiv);
} ( ) );
createElement, append, get link
text, button, click delete
Create a div on the page with id, and can be
deleted
javascript:(function()
{
function ourFunction()
{
let report;
let theLinks =
document.getElementsByTagName("a
");
let i;
for(i = 0; i < theLinks.length; i++)
{
report +=
theLinks[i].textContent;
report += "\n";
}
return report;
}
ourFunction();
let ourDiv =
document.createElement("button");
ourDiv.innerHTML = ourFunction();
ourDiv.id = "theDiv";
ourDiv.onclick= function(){ let
divRef=
document.getElementById("theDiv");
divRef.remove()};
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px
2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
document.body.append(ourDiv);
} ( ) );
createElement, append, Button, Get
URL of the Page you are on
Create a Button on the page, that gets the
URL of the page
javascript:(
function()
{
function ourFunction()
{
let ourButton =
document.createElement("BUTTON");
ourButton.innerHTML = 'Click for
URL';
ourButton.style.position = "absolute";
ourButton.style.top = "100px";
ourButton.style.left = "100px";
/* ourButton.style.width = "100px"; */
/* ourButton.style.height = "100px"; */
ourButton.style.padding = "2px
2px 2px 2px";
ourButton.style.background = "tan";
ourButton.style.color = "yellow";
ourButton.style.zIndex = "1000";
ourButton.onclick = function(){
function work(){ let theVideo = "hi";
alert(window.location.href);}work()};
document.body.append(ourButton);
}
ourFunction();
} ) ( );
createElement, append, Button,
Style, color, pos, layer, VIDEO
pause
Create a Button that Pauses the Video on the
page.
javascript:(
function()
{
function ourFunction()
{
let ourButton =
document.createElement("BUTTON");
ourButton.id = "theButton";
ourButton.style.position =
"absolute";
ourButton.style.top = "100px";
ourButton.style.left ="100px";
ourButton.style.padding = "2px
2px 2px 2px";
ourButton.style.background =
"tan";
ourButton.style.color = "yellow";
ourButton.style.zIndex ="1000";
ourButton.innerHTML = "Pause
Button";
ourButton.onclick = function(){
function work(){ let theVideo =
document.querySelectorAll("video")
[0]; theVideo.pause();} work() };
document.body.append(ourButton);
}
ourFunction();
} ) ( );
let ourPlayButton =
document.createElement("BUTTON");
ourPlayButton.id = "thePlayButton";
ourPlayButton.style.position = "absolute";
ourPlayButton.style.top = "200px";
ourPlayButton.style.left ="100px";
ourPlayButton.style.padding = "2px 2px
2px 2px";
ourPlayButton.style.background = "tan";
ourPlayButton.style.color = "yellow";
ourPlayButton.style.zIndex ="1000";
ourPlayButton.innerHTML = "Play Button";
document.body.append(ourPlayButton);
ourPlayButton.onclick = function(){
function playNow(){ let theVideo =
document.querySelectorAll("video")[0];
theVideo.play();} playNow(); };
}
ourFunction();
} ) ( );
createElement Buttons VIDEO, play,
pause, back, forward, Mute,
UnMute
Create Buttons that Control the Video on the
page
javascript:(function(){
function ourFunction()
{
let ourPlayButton =
document.createElement("BUTTON");
ourPlayButton.innerHTML = "Play";
ourPlayButton.id = "thePlayButton";
ourPlayButton.style.position = "absolute";
ourPlayButton.style.top = "100px";
ourPlayButton.style.left ="5px";
ourPlayButton.style.padding = "2px 2px
2px 2px";
ourPlayButton.style.background = "tan";
ourPlayButton.style.color = "yellow";
ourPlayButton.style.zIndex ="1000";
ourPlayButton.innerHTML = "Play";
ourPlayButton.onclick = function(){
function playNow(){ let theVideo =
document.querySelectorAll("video")[0];
theVideo.play();} playNow(); };
document.body.append(ourPlayButton);
let ourPauseButton =
document.createElement("BUTTON");
ourPauseButton.innerHTML = "Pause";
ourPauseButton.id = "theButton";
ourPauseButton.style.position =
"absolute";
ourPauseButton.style.top = "125px";
ourPauseButton.style.left ="5px";
ourPauseButton.style.padding = "2px
2px 2px 2px";
ourPauseButton.style.background = "tan";
ourPauseButton.style.color = "yellow";
ourPauseButton.style.zIndex ="1000";
ourPauseButton.innerHTML = "Pause";
ourPauseButton.onclick = function(){
function pauseNow() { let theVideo =
document.querySelectorAll("video")[0];
theVideo.pause();} pauseNow(); };
document.body.append(ourPauseButton);
let ourBackButton =
document.createElement("BUTTON");
ourBackButton.innerHTML = "Back";
ourBackButton.id = "theBackButton";
ourBackButton.style.position =
"absolute";
ourBackButton.style.top = "150px";
ourBackButton.style.left ="5px";
ourBackButton.style.padding = "2px
2px 2px 2px";
ourBackButton.style.background =
"tan";
ourBackButton.style.color = "yellow";
ourBackButton.style.zIndex ="1000";
ourBackButton.innerHTML = "Back";
ourBackButton.onclick = function(){
function backNow() { let theTime =
document.querySelector("video").currentTim
e += -2; } backNow()};
document.body.append(ourBackButton);
let ourForwardButton =
document.createElement("BUTTON");
ourForwardButton.innerHTML =
"Forward";
ourForwardButton.id =
"theForwardButton";
ourForwardButton.style.position =
"absolute";
ourForwardButton.style.top = "175px";
ourForwardButton.style.left ="5px";
ourForwardButton.style.padding = "2px
2px 2px 2px";
ourForwardButton.style.background =
"tan";
ourForwardButton.style.color =
"yellow";
ourForwardButton.style.zIndex ="1000";
ourForwardButton.innerHTML =
"Forward";
ourForwardButton.onclick = function(){
function backNow() { let theTime =
document.querySelector("video").currentTim
e += 2; } backNow()};
document.body.append(ourForwardButton);
let ourSpeedButton =
document.createElement("BUTTON");
ourSpeedButton.id = "theSpeedButton";
ourSpeedButton.style.position =
"absolute";
ourSpeedButton.style.top = "200px";
ourSpeedButton.style.left ="5px";
ourSpeedButton.style.padding = "2px
2px 2px 2px";
ourSpeedButton.style.background =
"tan";
ourSpeedButton.style.color = "yellow";
ourSpeedButton.style.zIndex ="1000";
ourSpeedButton.innerHTML = "Speed";
ourSpeedButton.onclick = function(){
function speedNow() { let theTime =
document.querySelector("video").playbackR
ate = parseFloat(prompt("Enter speed",
"0.50")); } speedNow(); };
document.body.append(ourSpeedButton);
let ourMuteButton =
document.createElement("BUTTON");
ourMuteButton.innerHTML = "MUTE";
ourMuteButton.id = "theMuteButton";
ourMuteButton.style.position =
"absolute";
ourMuteButton.style.top = "225px";
ourMuteButton.style.left ="5px";
ourMuteButton.style.padding = "2px 2px
2px 2px";
ourMuteButton.style.background =
"tan";
ourMuteButton.style.color = "yellow";
ourMuteButton.style.zIndex ="1000";
ourMuteButton.innerHTML = "Mute";
ourMuteButton.onclick= function(){
function muteNow() {
document.querySelector("video").muted=tru
e;} muteNow();};
document.body.append(ourMuteButton);
let ourUnMuteButton =
document.createElement("BUTTON");
ourUnMuteButton.id =
"theUnMuteButton";
ourUnMuteButton.style.position =
"absolute";
ourUnMuteButton.style.top = "250px";
ourUnMuteButton.style.left ="5px";
ourUnMuteButton.style.padding = "2px
2px 2px 2px";
ourUnMuteButton.style.background =
"tan"
ourUnMuteButton.style.color =
"yellow";
ourUnMuteButton.style.zIndex ="1000";
ourUnMuteButton.innerHTML =
"Unmute";
ourUnMuteButton.onclick= function(){
function unMuteNow() {
document.querySelector("video").muted=fal
se;} unMuteNow();};
document.body.append(ourUnMuteButton);
}
ourFunction();
} ) ( );
createElement div, from object data
Create a div based on object data
javascript:(function()
{
function ourFunction()
{
let myPet =
{
name: "Tassey the Turtle",
age: "20"
};
return myPet.name + " " +
myPet.age;
}
ourFunction();
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = ourFunction();
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
document.body.append(ourDiv);
} ( ) );
createElement div, from object
data, dot syntax of function
Create a div based on object data using dot
syntax of function
javascript:(function()
{
function ourFunction()
{
let myPet =
{
name: "Tassey",
age: "20"
};
return myPet;
}
ourFunction();
let ourDiv =
document.createElement("div");
ourDiv.innerHTML =
ourFunction().name + " " +
ourFunction().age;
ourDiv.id = "theDiv";
ourDiv.innerHTML =
ourFunction();
ourDiv.style.position =
"absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.padding = "2px 2px
2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
document.body.append(ourDiv);
} ( ) );
createElement div, from object
data, dot syntax of function
Create a div, object data, dot syntax, longer
example
javascript:(function()
{
function BikaChu()
{
let bikachu =
{
name: "Bikachu",
height: "1",
weight: "30",
speedMax:"40",
power: "Electricity",
level: "73",
color: "yellow",
friend: "Dash"
};
return bikachu;
}
BikaChu();
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = BikaChu().name + ", "
+
BikaChu().height + ", " +
BikaChu().weight + ", " +
BikaChu().speedMax + ", " +
BikaChu().power + ", " +
BikaChu().level + ", " +
BikaChu().color + ", " +
BikaChu().friend;
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
ourDiv.style.padding = "2px 2px 2px 2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.onclick= function(){ let divRef =
document.getElementById("theDiv");
divRef.remove();};
document.body.append(ourDiv);
} ( ) );
javascript:(function()
{
class Animal
{
constructor(kind, name, size,weight,
speed)
{
this.kind = kind;
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed + " mph";
}
eat(){
return 'eating';
}
saySpeed(){
return this.speed;
}
}
let ourDiv =
document.createElement("div");
ourDiv.innerHTML += felix.meow() +
'<br>';
ourDiv.innerHTML += felix.eat() + '<br>';
ourDiv.innerHTML += felix.sayKind()+
'<br>';
ourDiv.innerHTML += felix.tailWag()+
'<br>';
ourDiv.innerHTML += felix.saySpeed();
ourDiv.id = "theDiv";
ourDiv.style.position = "absolute";
ourDiv.style.top = "100px";
ourDiv.style.left ="100px";
/* ourDiv.style.width = "100px"; */
/* ourDiv.style.height = "100px"; */
ourDiv.style.padding = "2px 2px 2px
2px";
ourDiv.style.background = "tan";
ourDiv.style.color = "yellow";
ourDiv.style.zIndex ="1000";
ourDiv.onclick= function(){ let divRef =
document.getElementById("theDiv");
divRef.remove();};
document.body.append(ourDiv);
} ) ( );
Get Specific Button onclick value
Shows the onclick value of the first button
found
javascript:(
function()
{
let theButton =
document.getElementsByTagName
("BUTTON")[0];
let theName =
theButton.attributes.getNamedItem
("onclick").value;
alert(theName);
} ( ) );
javascript:(
function()
{
let theParagraphs =
document.getElementsByTagName("P");
let i;
for(i = 0; i < theParagraphs.length; i++)
{
theParagraphs[i].style.backgroundColor
= "aqua";
theParagraphs[i].style.fontWeight =
"bold";
theParagraphs[i].style.color = "black";
}
} ( ) );
Number of Buttons on a Page
Shows How many Buttons are on the page
javascript:(
function()
{
let theButtons =
document.getElementsByTagNa
me("Button");
alert(theButtons.length);
} ( ) );
Number of Links on a Page
Shows How many links are on a page
javascript:(
function()
{
let theLinks =
document.getElementsByTa
gName("a");
alert(theLinks.length);
} ( ) );
Show the InnerHTML of each
Paragraph on a Page
Find all paragraphs on a page
javascript:(
function()
{
let report;
let theParagraphs =
document.getElementsByTagName
("P");
let i;
for(i=0; i<theParagraphs.length;
i++)
{
report +=
theParagraphs[i].innerHTML;
report += "\n";
}
alert(report);
} ( ) );
Show the InnerHTML of each
Paragraph with Style
Find all paragraphs on a page and style them
javascript:(
function()
{
let report;
let theParagraphs =
document.getElementsByTagName
("P");
let i;
for(i=0; i
<theParagraphs.length; i++)
{
theParagraphs[i].style.background
Color = "aqua";
theParagraphs[i].style.fontWeight =
"bold";
theParagraphs[i].style.color =
"black";
report +=
theParagraphs[i].innerHTML;
report += "\n";
}
alert(report);
} ( ) );
Show the href of each Link on Page
Find all links on a page
javascript:(
function()
{
let report;
let theLinks =
document.getElementsByTagName
("A");
let i;
for(i = 0; i < theLinks.length; i++)
{
report += theLinks[i].href;
report += "\n";
}
alert(report);
} ( ) );
Show the href of each Link with
Style
Find all links on a page and style them
javascript:(
function()
{
let report;
let theLinks =
document.getElementsByTagName("A");
let i;
javascript:(
function()
{
let report;
let theLinks =
document.getElementsByTagName("img
");
let i;
javascript:(
function()
{
let report;
let theLinks =
document.getElementsByTagName
("img");
let i;
javascript:(
function()
{
let report;
let theButtons =
document.getElementsByTagNa
me("Button");
let i;
for(i = 0; i <
theButtons.length; i++)
{
report += theButtons[i].id;
report += "\n";
}
alert(report);
} ( ) );
Show the Links id’s on a Page
Find all links on a page and show their id
javascript:(
function()
{
let report;
let theLinks =
document.getElementsByTagName
("a");
let i;
javascript:(function(){
let nameList =
document.querySelectorAll("H1");
let i;
let thenames=[ ];
alert(thenames.toString());
} ( ) );
Show elements of a certain class on
a Website
Display elements from Weebly website of a
certain class name.
javascript:(function(){
let nameList =
document.querySelectorAll(".wsite
-content-title");
let i;
let thenames=[ ];
for(i=0; i<nameList.length; i++)
{
thenames +=
nameList[i].textContent;
thenames += "\n";
}
alert(thenames.toString());
} ( ) );
The Bookmarklet on the previous page is
designed to work ONLY on Weebly Websites.
Go to http://CollegeOfScripting.weebly.com
Right Click on the Titles, such as JavaScript
Choose Inspect Element
javascript:(
function()
{
function ourFunction()
{
alert("Howdy");
}
setTimeout(ourFunction, 5000);
} ( ) );
Timer – Every 5 Seconds, Trigger
function
Timer activates every 5 seconds
javascript:(
function()
{
function ourFunction()
{
alert("Howdy");
}
setInterval(ourFunction,
5000);
} ( ) );
Timer – Every 1 Second, COUNT UP
Timer activates every 1 seconds
javascript:(
function()
{
let counter = 0;
function openIt()
{
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = updateIt();
ourDiv.id = "theDiv";
document.body.append(ourDiv);
ourDiv.setAttribute('style','backgroun
d-
color:black;position:absolute;left:20p
x;top:150px;color:white;z-
index:1;width:100px;height:25px;font-
size:19px; padding:8px;');
}
function updateIt()
{
counter += 1;
return counter;
}
openIt();
setInterval(openIt, 1000);
} ) ( );
Timer – Every 1 Second, COUNT
DOWN, clearInterval
Timer Counts Down Starting at Number of
Seconds User Entered
javascript:(
function()
{
let counter = prompt("Enter
Count Down Start Time");
function openIt()
{
let ourDiv =
document.createElement("div");
ourDiv.innerHTML =
updateIt();
ourDiv.id = "theDiv";
document.body.append(ourDiv);
ourDiv.setAttribute('style','backgro
und-
color:black;position:absolute;left:2
0px;top:150px;color:white;z-
index:1;width:100px;height:25px;fo
nt-size:19px; padding:8px;');
}
function updateIt() {
counter -= 1;
if(counter == 0)
{
clearInterval(ourTimer);
}
return counter;
}
openIt();
let ourTimer =
setInterval(openIt, 1000);
} ) ( );
Timer – Every 1 Second, Trigger
Military Time
Timer activates every 1 seconds
javascript:(
function()
{
function openIt()
{
let ourDiv =
document.createElement("div");
ourDiv.innerHTML = updateIt();
ourDiv.id = "theDiv";
document.body.append(ourDiv);
ourDiv.setAttribute('style','background-
color:black;position:absolute;left:20px;t
op:150px;color:white;z-
index:1;width:100px;height:25px;font-
size:19px; padding:8px;');
}
function dateTime()
{
let date = new Date();
return date;
}
function hours()
{
let theHours =
dateTime().getHours();
return theHours;
}
function minutes()
{
let theMinutes =
dateTime().getMinutes();
return theMinutes;
}
function seconds()
{
let theSeconds =
dateTime().getSeconds();
return theSeconds;
}
function updateIt()
{
return hours().toString() + ":" +
minutes().toString()+ ":" +
seconds().toString();
}
openIt();
setInterval(openIt, 1000);
} ) ( );
Calculate DISTANCE
DISTANCE = SPEED * TIME
javascript:(
function()
{
function ourFunction(mph, hours)
{
mph = prompt("Enter Mph");
hours = prompt("EnterHours");
let time = mph * hours;
return String(time) + " Miles";
}
alert(ourFunction());
} ) ( );
DISTANCE
SPEED * TIME
The Distance in Miles,
Equals the Speed in Miles Per Hours,
Multiplied by the Time in Hours.
Calculate SPEED
SPEED = DISTANCE / TIME
javascript:(
function()
{
function ourFunction(miles, hours)
{
miles = prompt("Enter Miles");
hours = prompt("Enter Hours");
javascript:(
function()
{
function ourFunction(seconds)
{
seconds = prompt("Enter Seconds");
javascript:(
function()
{
function ourFunction(minutes)
{
minutes= prompt("Enter
Minutes");
javascript:(
function()
{
function ourFunction(price,
taxRate)
{
price = prompt("Enter
Price");
taxRate /= 100;
javascript:(
function()
{
let average;
function ourFunction(a,b,c)
{
a = prompt("Enter 1st number");
b = prompt("Enter 2nd number");
c = prompt("Enter 3rd number");
javascript:(
function()
{
function ourFunction(a,b)
{
a = prompt("Enter 1st number");
b = prompt("Enter 2nd number");
javascript:(
function() {
function ourFunction(side)
{
side = prompt("Enter Length in
Meters");
10 m x 10 m =
100 Square m
SIDE B
Calculate PERIMETER of a SQUARE
PERIMETER OF SQUARE = SIDE * 4
javascript:(
function() {
function ourFunction(side)
{
side = prompt("Enter Length in Feet");
let perimeter = side * 4;
perimeter = Math.round(perimeter);
return String(perimeter + "feet");
}
alert(ourFunction());
} ) ( );
SIDE A 10 feet
10 Feet
10 Feet
10 Feet x 4 =
40 Feet
SIDE B
SIDE B
Perimeter
SIDE A 10 feet
Convert FRACTION to DECIMAL
DECIMAL=NUMERATOR / DENOMINATOR
javascript:(
function()
{
function ourFunction(numerator,
denominator)
{
numerator = prompt("Enter
Numerator");
denominator = prompt("Enter
Denominator");
javascript:(
function()
{
function ourFunction(numerator,
denominator)
{
numerator = prompt("Enter
Numerator");
denominator = prompt("Enter
Denominator");
let percent = (numerator /
denominator)*100;
return String(percent + "%");
}
alert(ourFunction());
} ) ( );
Convert PERCENT to DECIMAL
DECIMAL = PERCENT / 100
javascript:(
function()
{
function ourFunction(percent)
{
percent = prompt("Enter
Percent");
return String(decimal);
}
alert(ourFunction());
} ) ( );
Convert DECIMAL to PERCENT
PERCENT = DECIMAL * 100
javascript:(
function()
{
function ourFunction(decimal)
{
decimal = prompt("Enter
Decimal");
javascript:(
function()
{
function ourFunction(meters)
{
meters = prompt("Enter Meters");
javascript:(
function()
{
function ourFunction(inches)
{
inches = prompt("Enter Inches");
javascript:(
function()
{
function ourFunction(feet)
{
feet = prompt("Enter Feet");
javascript:(
function()
{
function ourFunction(feet, inches)
{
feet = prompt("Enter Feet");
inches = prompt("Enter Inches");
feet *= 0.3048;
inches *= 0.0254;
javascript:(
function()
{
function ourFunction(hours)
{
hours = prompt("Enter
Hours");
javascript:(
function()
{
function ourFunction(minutes)
{
minutes = prompt("Enter Minutes");
javascript:(
function()
{
function ourFunction(seconds)
{
seconds = prompt("Enter
Seconds");
javascript:(
function()
{
function ourFunction(fahrenheit)
{
fahrenheit = prompt("Enter
Temperature in Fahrenheit");
<html>
<body>
<script>
function simple()
{
return "howdy";
}
alert(simple());
</script>
</body>
</html>
Console Log, Stringify All Records
<html><head>
<title> Console Log, Stringify
All Records </title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
weight: 140
},
{
name: "Donald",
weight: 160
}
];
document.write(JSON.stringify(
myRecord));
</script>
<div
id="theJsonOutput"></div>
</body>
</html>
Console Log, Stringify One Record
<html><head>
<title> Console Log, Stringify
One Record </title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
weight: 140
},
{
name: "Donald",
weight: 160
}
];
document.write(JSON.stringify(
myRecord));
</script>
<div
id="theJsonOutput"></div>
</body></html>
or use
console.log(JSON.stringify
(myRecord[1]));
document.write(JSON.stringify(
myRecord[1]));
Display One Record using innerHTML
<html><head>
<title> Display One Record Using
innerHTML </title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
height: "5 feet 9 3/4 inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish, Other"
},
{
name: "Donald",
height: "6 feet 4 inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonO
utput");
jsonOutputDiv.innerHTML =
myRecord[1].name;
}
</script>
<button id="insertIt"
onclick="renderHtml()">
Insert to page
</button>
<div id="theJsonOutput"></div>
</body>
</html>
Display All Records, innerHTML
<html><head>
<title>Display All Records using
innerHTML </title>
</head>
<body>
<script>
let myRecord = [
{
name: "John",
height: "5 Feet 10 Inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish"
},
{
name: "Donald",
height: "6 Feet 4 Inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonO
utput");
jsonOutputDiv.innerHTML =
JSON.stringify(myRecord);
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to page
</button>
<div id="theJsonOutput"></div>
</body>
</html>
for loop ALL Records
<html><head><title> for loop through All
Records </title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
height: "5 feet 9 3/4 inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish, Other"
},
{
name: "Donald",
height: "6 feet 4 inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonOutp
ut");
<script>
let myRecord = [
{
name: "Key",
height: "5 feet 9 3/4 inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish"
},
{
name: "Donald",
height: "6 feet 4 inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonOutp
ut");
let output = '';
for(let i = 0; i < myRecord.length; i+
+)
{
output +=
JSON.stringify(myRecord[i]);
output += '<br>';
}
jsonOutputDiv.innerHTML = output;
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to
page</button>
<div id="theJsonOutput"></div>
</body>
</html>
for loop ALL Records, Stringify, List
<html><head>
<title> for loop through ALL Records,
Stringify, List </title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
height: "5 feet 9 3/4 inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish"
},
{
name: "Donald",
height: "6 feet 4 inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonOutp
ut");
let output = '';
for(let i = 0; i < myRecord.length; i+
+)
{
output += '<li>'
+myRecord[i].name+ '</li>';
}
jsonOutputDiv.innerHTML = output;
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to
page</button>
<div id="theJsonOutput"></div>
</body>
</html>
<!--
For a uniform list use:
output += '<ul><li>' +myRecord[i].name+
'</li></ul>';
-->
for loop ALL Records, Select Items,
Spaces, New Line
<html><head>
<title>for loop ALL Records, Select Items,
Spaces, New Line</title>
</head>
<body>
<script>
let myRecord = [
{
name: "Key",
height: "5 feet 9 3/4 inches",
weight: 140,
eyeColor: "Brown",
nationality: "French, Irish"
},
{
name: "Donald",
height: "6 feet 4 inches",
weight: 160,
eyeColor: "Blue",
nationality: "Ukrainian, French"
}
];
function renderHtml()
{
let jsonOutputDiv =
document.getElementById("theJsonOutput")
;
let output = '';
for(let i = 0; i < myRecord.length; i++)
{
output += myRecord[i].name + " ";
output += myRecord[i].height + " ";
output += myRecord[i].weight +
'<br>';
}
jsonOutputDiv.innerHTML = output;
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to
page</button>
<div id="theJsonOutput"></div>
</body>
</html>
or we could instead use:
jsonOutputDiv.innerHTML =
JSON.stringify(output);
//alert(JSON.stringify(myRecord[i]));
}
}
jsonOutputDiv.innerHTML = output;
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to
page</button>
<div id="theJsonOutput"></div>
</body>
</html>
if(myRecord[i].name.indexOf("Donald") != -1)
{
output += myRecord[i].name + ", ";
output += myRecord[i].height + ", ";
output += myRecord[i].weight + " "
+ "lbs, ";
output += myRecord[i].year;
output += '<br>';
}
}
}
jsonOutputDiv.innerHTML = output;
}
</script>
<button id="insertIt"
onclick="renderHtml()">Insert to
page</button>
<div id="theJsonOutput"></div>
</body></html>
Date and Time Simple
<html><head>
<title>Date and Time</title>
</head>
<body>
<button type="button"
onclick="document.getElementById('
displayDateTime').innerHTML=Date()"
>
Date and Time</button>
<div id="displayDateTime"></div>
</body>
</html>
Date Object - today's date and time
<html><script>
function TodaysDate()
{
let today = new Date();
let todayCleaner = new
Date().toLocaleDateString();
document.getElementById("date
Info").innerHTML = today;
document.getElementById("dateC
leaner").innerHTML =
todayCleaner;
}
</script>
<body>
<button
onclick="TodaysDate();">
Today's Date and Time
</button>
<div id="dateInfo"></div>
<br>
<div id="dateCleaner"></div>
</body>
</html>
document.getElementById(where).innerH
TML = whichOne;
}
</script>
</head>
<body>
<button onclick="displayIt('div1',
Hours());"> Hours </button>
<button onclick="displayIt('div2',
Minutes());"> Minutes </button>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
Functions, return, Display Time to
One Div
<html><head>
<title>Functions Using return, Display
to One Div</title>
<script>
function dateTime() //GET DATE TIME
{
let date = new Date();
return date;
}
function Hours() //GET HOURS
{
let theHours =
dateTime().getHours();
return theHours;
}
function Minutes() //GET MINUTES
{
let theMinutes =
dateTime().getMinutes();
return theMinutes;
}
function displayIt(whichOne) //DISPLAY
ANSWER
{
document.getElementById("displayAnsw
er").innerHTML = whichOne;
}
</script>
</head>
<body>
<button onclick="displayIt(Hours());">
Hours </button>
<button
onclick="displayIt(Minutes());"> Minutes
</button>
<div id="displayAnswer"></div>
</body>
</html>
Get Day and Show Message
<html><head><title>Function
Using return</title>
<script>
function theDayOfWeek()
{
let date = new Date();
let dayName;
if(day == 0)
{
dayName = "Sunday";
}
else if(day == 1)
{
dayName = "Monday";
}
else if(day == 2)
{
dayName = "Tuesday";
}
else if(day == 3)
{
dayName = "Wednesday";
}
else if(day == 4)
{
dayName = "Thursday";
}
else if(day == 5)
{
dayName = "Friday";
}
else if(day == 6)
{
dayName = "Saturday";
}
return dayName;
}
function messageFromDay()
{
let dayMessage;
if(theDayOfWeek() ==
"Friday")
{
dayMessage = "It's Friday.
Do something new";
}
else if(theDayOfWeek() ==
"Saturday")
{
dayMessage = "It's
Saturday. Good time for Scripting";
}
else if(theDayOfWeek() ==
"Sunday")
{
dayMessage = "It's Sunday.
Happy Scripting";
}
else if(theDayOfWeek() ==
"Monday")
{
dayMessage = "It's Monday.
Time to learn";
}
else if(theDayOfWeek() ==
"Tuesday")
{
dayMessage = "It's Tuesday.
Learning alot of Javascript";
}
else if(theDayOfWeek() ==
"Wednesday")
{
dayMessage = "It's
Wednesday. JSON is fun to learn";
}
else if(theDayOfWeek() ==
"Thursday")
{
dayMessage = "It's
Thursday. One more day till
Friday";
}
document.getElementById("dayMe
ssage").innerHTML = dayMessage;
}
</script>
</head>
<body onload =
"messageFromDay();">
<div id="dayMessage"></div>
</body>
</html>
Get Month and Show Message
<html><head><title>Function Using return -
Date Object getMonth</title>
<script>
function theMonth()
{
let date = new Date();
let monthName = date.getMonth();
// 0 to 11 0 is jan 11 is december
if(monthName == 0)
{
monthName = "January";
}
else if(monthName == 1)
{
monthName = "February";
}
else if(monthName == 2)
{
monthName = "March";
}
else if(monthName == 3)
{
monthName = "April";
}
else if(monthName == 4)
{
monthName = "May";
}
else if(monthName == 5)
{
monthName = "June";
}
else if(monthName == 6)
{
monthName = "July";
}
else if(monthName == 7)
{
monthName = "August";
}
else if(monthName == 8)
{
monthName = "September";
}
else if(monthName == 9)
{
monthName = "October";
}
else if(monthName == 10)
{
monthName = "November";
}
else if(monthName == 11)
{
monthName = "December";
}
return monthName;
}
function messageMonth()
{
let monthMessage;
if(theMonth() == "March")
{
monthMessage = "It's March. Spring
has come.";
}
else if(theMonth() == "April")
{
monthMessage = "It's April. Getting
Warmer out";
}
else if(theMonth() == "May")
{
monthMessage = "It's May. Very nice
out there now";
}
else if(theMonth() == "June")
{
monthMessage = "It's June. Summer
is here";
}
else if(theMonth() == "July")
{
monthMessage = "It's July. A Nice
Warm Summer so far";
}
else if(theMonth() == "August")
{
monthMessage = "It's August.
Summer is almost over";
}
else if(theMonth() == "September")
{
monthMessage = "It's September.
Autumn has come";
}
else if(theMonth() == "October")
{
monthMessage = "It's October. It's
getting cooler out there";
}
else if(theMonth() == "November")
{
monthMessage = "It's November.
Winter is almost here";
}
else if(theMonth() == "December")
{
monthMessage = "It's December.
Winter is here";
}
else if(theMonth() == "January")
{
monthMessage = "It's January. It's a
snowy winter";
}
else if(theMonth() == "February")
{
monthMessage = "It's February. So
much snow";
}
document.getElementById("monthDisplay").i
nnerHTML = monthMessage;
}
</script>
</head>
<html><head>
<title>Function Using return - Date Object
getHours</title>
<script>
function theHours()
{
let date = new Date();
let hours = date.getHours(); //0 to 23
return hours;
}
function messageHours()
{
let hoursMessage;
console.log(theHours());
document.getElementById("hoursDisplay").i
nnerHTML = hoursMessage;
}
</script>
</head>
<div id="hoursDisplay"></div>
</body>
</html>
Date and Time - Complete Time
Code Script
<html><head>
<title> Date and Time </title>
<style>
.myText
{
font-size: 19px;
height: 22px;
width: 480px;
resize: none;
background-color: black;
color: aqua;
text-align: center;
font-weight: bold;
padding-top:1px;
border-color:grey;
border-radius: 4px;
margin-bottom:5px;
}
div
{
color:blue;
font-weight: bold;
font-size: 22px;
}
</style>
<script>
function theFullDate()
{
let date = new Date();
}
function theDayOfWeek()
{
let date = new Date();
let day = date.getDay();
// 0 to 6 0 is sunday 6 is saturday
let dayName;
if(day == 0)
{
dayName = "Sunday";
}
else if(day == 1)
{
dayName = "Monday";
}
else if(day == 2)
{
dayName = "Tuesday";
}
else if(day == 3)
{
dayName = "Wednesday";
}
else if(day == 4)
{
dayName = "Thursday";
}
else if(day == 5)
{
dayName = "Friday";
}
else if(day == 6)
{
dayName = "Saturday";
}
return dayName;
}
function theMonth()
{
let date = new Date();
let monthName = date.getMonth();
// 0 to 11 0 is jan 11 is december
if(monthName == 0)
{
monthName = "January";
}
else if(monthName == 1)
{
monthName = "February";
}
else if(monthName == 2)
{
monthName = "March";
}
else if(monthName == 3)
{
monthName = "April";
}
else if(monthName == 4)
{
monthName = "May";
}
else if(monthName == 5)
{
monthName = "June";
}
else if(monthName == 6)
{
monthName = "July";
}
else if(monthName == 7)
{
monthName = "August";
}
else if(monthName == 8)
{
monthName = "September";
}
else if(monthName == 9)
{
monthName = "October";
}
else if(monthName == 10)
{
monthName = "November";
}
else if(monthName == 11)
{
monthName = "December";
}
return monthName;
}
function theDayOfMonth()
{
let date = new Date();
let dayOfMonth = date.getDate();
//1 to 31
return dayOfMonth;
}
function theYear()
{
let date = new Date();
let year = date.getFullYear();
return year;
}
function getMilitaryHours()
{
let date = new Date();
let hours = date.getHours();
//0 to 23
return hours;
}
function theHours()
{
let date = new Date();
let hours = date.getHours();
//0 to 23
if(hours == 0)
{
hours = 12; //am
}
else if(hours == 1)
{
hours = 1; //am
}
else if(hours == 2)
{
hours = 2; //am
}
else if(hours == 3)
{
hours = 3; //am
}
else if(hours == 4)
{
hours = 4; //am
}
else if(hours == 5)
{
hours = 5; //am
}
else if(hours == 6)
{
hours = 6; //am
}
else if(hours == 7)
{
hours = 7; //am
}
else if (hours == 8)
{
hours = 8; //am
}
else if(hours == 9)
{
hours = 9; //am
}
else if(hours == 10)
{
hours = 10; //am
}
else if(hours == 11)
{
hours = 11; //am
}
else if(hours == 12)
{
hours = 12; //pm
}
else if(hours == 13)
{
hours = 1; //pm
}
else if(hours == 14)
{
hours = 2; //pm
}
else if (hours == 15)
{
hours = 3; //pm
}
else if (hours == 16)
{
hours = 4; //pm
}
else if (hours == 17)
{
hours = 5; //pm
}
else if (hours == 18)
{
hours = 6; //pm
}
else if(hours == 19)
{
hours = 7; //pm
}
else if(hours == 20)
{
hours = 8; //pm
}
else if (hours == 21)
{
hours = 9; //pm
}
else if (hours == 22)
{
hours = 10; //pm
}
else if (hours == 23)
{
hours = 11; //pm
}
return hours;
}
function theMinutes()
{
let date = new Date();
let minutes = date.getMinutes();
if(minutes==0)
{
return minutes = "00";
}
else if(minutes == 1)
{
return minutes = "01";
}
else if(minutes == 2)
{
return minutes = "02";
}
else if(minutes == 3)
{
return minutes = "03";
}
else if(minutes == 4)
{
return minutes = "04";
}
else if(minutes == 5)
{
return minutes = "05";
}
else if(minutes == 6)
{
return minutes = "06";
}
else if(minutes == 7)
{
return minutes = "07";
}
else if(minutes == 8)
{
return minutes = "08";
}
else if(minutes == 9)
{
return minutes = "09";
}
return minutes;
}
function amOrpm()
{
let date = new Date();
let hours = date.getHours(); //0 to 23
let halfOfDay;
if(hours == 0)
{
halfOfDay = "am";
}
else if(hours == 1)
{
halfOfDay = "am";
}
else if(hours == 2)
{
halfOfDay = "am";
}
else if(hours == 3)
{
halfOfDay = "am";
}
else if(hours == 4)
{
halfOfDay = "am";
}
else if(hours == 5)
{
halfOfDay = "am";
}
else if(hours == 6)
{
halfOfDay = "am";
}
else if(hours == 7)
{
halfOfDay = "am";
}
else if(hours == 8)
{
halfOfDay = "am";
}
else if(hours == 9)
{
halfOfDay = "am";
}
else if (hours == 10)
{
halfOfDay = "am";
}
else if(hours == 11)
{
halfOfDay = "am";
}
else if(hours == 12)
{
halfOfDay = "pm";
}
else if(hours == 13)
{
halfOfDay = "pm";
}
else if(hours == 14)
{
halfOfDay = "pm";
}
else if(hours == 15)
{
halfOfDay = "pm";
}
else if(hours == 16)
{
halfOfDay = "pm";
}
else if(hours == 17)
{
halfOfDay = "pm";
}
else if(hours == 18)
{
halfOfDay = "pm";
}
else if(hours == 19)
{
halfOfDay = "pm";
}
else if(hours ==20)
{
halfOfDay = "pm";
}
else if(hours == 21)
{
halfOfDay = "pm";
}
else if (hours == 22)
{
halfOfDay = "pm";
}
else if (hours == 23)
{
halfOfDay = "pm";
}
return halfOfDay;
}
function theSeconds()
{
let date = new Date();
let seconds = date.getSeconds();
if(seconds==0)
{
return seconds = "00";
}
else if(seconds == 1)
{
return seconds = "01";
}
else if(seconds == 2)
{
return seconds = "02";
}
else if(seconds == 3)
{
return seconds = "03";
}
else if(seconds == 4)
{
return seconds = "04";
}
else if(seconds == 5)
{
return seconds = "05";
}
else if(seconds == 6)
{
return seconds = "06";
}
else if(seconds == 7)
{
return seconds = "07";
}
else if(seconds == 8)
{
return seconds = "08";
}
else if(seconds == 9)
{
return seconds = "09";
}
return seconds;
}
function keepGettingTime()
{
timeFunctionUpdates();
t=
setInterval('timeFunctionUpdates()',500);
}
function keepGettingMilitaryTime()
{
timeFunctionUpdatesMilitary();
t=
setInterval('timeFunctionUpdatesMilitary(
)',500);
}
function timeFunction(dayName,
monthName, dayOfMonth, year, hours,
minutes, halfOfDay)
{
document.getElementById("displayTime
Function").innerHTML =
theDayOfWeek(dayName) + ", " +
theMonth(monthName) + " " +
theDayOfMonth(dayOfMonth) +", " +
theYear(year) + " at " + theHours(hours)
+ ":" + theMinutes(minutes) + "" +
amOrpm(halfOfDay);
}
function
timeFunctionStart(dayName,monthName,
dayOfMonth,
year,hours,minutes,halfOfDay)
{
document.getElementById("displayTime
FunctionStart").innerHTML=
theDayOfWeek(dayName) + ", " +
theMonth(monthName) + " " +
theDayOfMonth(dayOfMonth) + ", " +
theYear(year) + " at " + theHours(hours)
+ ":" + theMinutes(minutes) + "" +
amOrpm(halfOfDay);
}
function
timeFunctionEnd(dayName,monthName,
dayOfMonth,year,
hours,minutes,halfOfDay)
{
document.getElementById("displayTime
FunctionEnd").innerHTML =
theDayOfWeek(dayName) + ", " +
theMonth(monthName) + " " +
theDayOfMonth(dayOfMonth) + ", " +
theYear(year) + " at " + theHours(hours)
+ ":" + theMinutes(minutes) + "" +
amOrpm(halfOfDay);
}
function
timeFunctionUpdates(dayName,monthNa
me,dayOfMonth,year,hours,minutes,seco
nds,halfOfDay)
{
document.getElementById("displayTime
FunctionUpdated").innerHTML =
theDayOfWeek(dayName) + ", " +
theMonth(monthName) + " " +
theDayOfMonth(dayOfMonth) + ", " +
theYear(year) + " at " + theHours(hours)
+ ":" + theMinutes(minutes) + ":" +
theSeconds(seconds) + " " +
amOrpm(halfOfDay);
}
function
timeFunctionUpdatesMilitary(dayName,m
onthName,dayOfMonth,year,hours,minut
es,seconds,halfOfDay)
{
document.getElementById("displayTime
FunctionUpdatedMilitary").innerHTML =
theDayOfWeek(dayName) + ", " +
theMonth(monthName) + " " +
theDayOfMonth(dayOfMonth) + ", " +
theYear(year) + " at " +
getMilitaryHours(hours) + ":" +
theMinutes(minutes) + ":" +
theSeconds(seconds) + " " +
amOrpm(halfOfDay);
}
function messageFromTime()
{
let dayMessage;
if(theDayOfWeek() == "Friday")
{
dayMessage = "It's Friday. Good
time to learn something new.";
}
else if(theDayOfWeek() ==
"Saturday")
{
dayMessage = "It's Saturday.
Good time for Scripting.";
}
else if(theDayOfWeek() ==
"Sunday")
{
dayMessage = "It's Sunday.
Happy Scripting.";
}
else if(theDayOfWeek() ==
"Monday")
{
dayMessage = "It's Monday. Time
to learn.";
}
else if(theDayOfWeek() ==
"Tuesday")
{
dayMessage = "It's Tuesday.
Learning alot of Javascript.";
}
else if(theDayOfWeek() ==
"Wednesday")
{
dayMessage = "It's Wednesday.
JSON is fun to learn.";
}
else if(theDayOfWeek() ==
"Thursday")
{
dayMessage = "It's Thursday. One
more day till Friday.";
}
document.getElementById("dayMessage
").innerHTML = dayMessage;
}
function messageMonth()
{
let monthMessage;
if(theMonth() == "March")
{
monthMessage = "It's March.
Spring has come.";
}
else if(theMonth() == "April")
{
monthMessage = "It's April.
Getting Warmer out.";
}
else if(theMonth() == "May")
{
monthMessage = "It's May. Very
nice out there now.";
}
else if(theMonth() == "June")
{
monthMessage = "It's June.
Summer is here.";
}
else if(theMonth() == "July")
{
monthMessage = "It's July. A Nice
Warm Summer so far.";
}
else if(theMonth() == "August")
{
monthMessage = "It's August.
Summer is almost over.";
}
else if(theMonth() == "September")
{
monthMessage = "It's September.
Autumn has come.";
}
else if(theMonth() == "October")
{
monthMessage = "It's October. It's
getting cooler out there.";
}
else if(theMonth() == "November")
{
monthMessage = "It's November.
Winter mis almost here.";
}
else if(theMonth() == "December")
{
monthMessage = "It's December.
Winter is here.";
}
else if(theMonth() == "January")
{
monthMessage = "It's January. It's
not a snowy winter.";
}
else if(theMonth() == "February")
{
monthMessage = "It's February.
There is not much snow out there.";
}
document.getElementById("monthDispla
y").innerHTML = monthMessage;
}
function theSimpleHours()
{
let date = new Date();
let hours = date.getHours(); //0 to 23
return hours;
}
function messageHours()
{
let hoursMessage;
console.log(theSimpleHours());
if(theSimpleHours() >= 0 &&
theSimpleHours() < 6)
{
hoursMessage = "It's Early
Morning.";
}
else if(theSimpleHours() >= 6 &&
theSimpleHours() < 12)
{
hoursMessage = "It's Mid
Morning.";
}
else if(theSimpleHours() >= 12 &&
theSimpleHours() < 18)
{
hoursMessage = "It's Afternoon.";
}
else if(theSimpleHours() >= 18 &&
theSimpleHours() <= 23)
{
hoursMessage = "It's night time.";
}
document.getElementById("hoursDispla
y").innerHTML = hoursMessage;
}
</script>
</head>
<body onload = "keepGettingTime();
messageFromTime(); messageMonth();
messageHours();">
<div>
<textarea
id="displayTimeFunctionUpdated"
class="myText" rows="1"
cols="9"></textarea>
</div>
<div id="dayMessage"></div>
<div id="monthDisplay"></div>
<div id="hoursDisplay"></div>
</body>
</html>
Output of the Code
It's Thursday.
One more day till Friday.
It's May.
Very nice out there now.
It's Mid Morning.
For More Tutorials:
CollegeOfScripting.weebly.com
CollegeOfScripting.wordpress.com
Youtube.com/ScriptingCollege
Github.com/ChristopherTopalian
Dedicated to God the Father
This book is created by the
College of Scripting Music & Science
Always remember, that each time you write a
script with a pencil and paper, it becomes
imprinted so deeply in memory that the
material and methods are learned extremely
well.
When you Type the scripts, the same is true.
The more you type and write out the scripts
by keyboard or pencil and paper, the more
you will learn programming!
Write and Type EVERY example that you find.
Keep all of your scripts organized.
Every script that you create increases your
programming abilities.
SEEING CODE, is one thing,
but WRITING CODE is another.
Write it, Type it, Speak It, See It, Dream It.
www.CollegeOfScripting.weebly.com