Alert on button press
Monday, February 23rd, 2009This simply pops up an alert box when you push a button
| programmershelp.co.uk |
| Javascript Resources |
This simply pops up an alert box when you push a button
enter this in the <HEAD> section
<SCRIPT>
<!–
var onHours = ” “;
var onMinutes = ” “;
var onSeconds = ” “;
var offHours = 0;
var offMinutes = 0;
var offSeconds = 0;
var logSeconds = 0;
var logMinutes = 0;
var logHours = 0;
var OnTimeValue = ” “;
var OffTimeValue = ” “;
var PageTimeValue = ” “;
// Back to previous page.
function WinOpen()
{
getLogoffTime();
window.location=”js-timet.htm”
window.location=”js-timet.htm” // double callfor Mac users??
}
// Loads HTML page to full window for View Source.
function WinOpen1()
{
alert(’View/Document Source from menu bar…, then close
new window to return to this page. The animation of the
radio buttons on the JavaScript index will be dead upon
return. Reload the JavaScript page to get them going again.
‘);
window.open(”js-timer.htm”,”DisplayWindow”,”menubar=yes,scrollbars=yes”);
}
// Captures logon time.
function getLogonTime()
{
var now = new Date();
// Used to display logon time.
var ampm = (now.getHours() >= 12) ? ” P.M.” : ” A.M.”;
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? “:0″
: “:”) + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? “:0″
: “:”) + now.getSeconds();
// String to display log-on time.
OnTimeValue =(” ” + Hours + Minutes + Seconds
+ ” ” + ampm);
// Capture logon time for use in timer().
onHours = now.getHours();
onMinutes = now.getMinutes();
onSeconds = now.getSeconds();
}
function getLogoffTime()
{
var now = new Date();
// Used to display logoff time.
var ampm = (now.getHours() >= 12) ? ” P.M.”
: ” A.M.”;
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? “:0″
: “:”) + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? “:0″
: “:”) + now.getSeconds();
// String to display log-off time.
OffTimeValue =(” ” + Hours + Minutes + Seconds
+ ” ” + ampm);
// Capture logoff time for use in timer().
offHours = now.getHours();
offMinutes = now.getMinutes();
offSeconds = now.getSeconds();
timer();
}
// Compute difference between logoff time and logon time.
function timer()
{
if (offSeconds >= onSeconds)
{ logSeconds = offSeconds - onSeconds; }
else
{
offMinutes -= 1;
logSeconds = (offSeconds + 60) - onSeconds;
}
if (offMinutes >= onMinutes)
{ logMinutes = offMinutes - onMinutes; }
else
{
offHours -= 1;
logMinutes = (offMinutes + 60) - onMinutes;
}
logHours = offHours - onHours;
// Used to display time on page.
logHours = ((logHours < 10) ? “0″ : “:”)
+ logHours;
logMinutes = ((logMinutes < 10) ? “:0″ : “:”)
+ logMinutes;
logSeconds = ((logSeconds < 10) ? “:0″ : “:”)
+logSeconds;
// String to display time on page.
PageTimeValue =(” ” + logHours + logMinutes +
logSeconds);
displayTimes();
}
function displayTimes()
{
alert(”\nLOG ON TIME : ” +OnTimeValue+”\n\nLOG
OFF TIME : “+OffTimeValue+”\n\nTIME ON PAGE :
” + PageTimeValue);
}
// –>
</SCRIPT>
now modify the <BODY> tag like this
<body onLoad=”getLogonTime()” ;>
and here is the code for the push button
<INPUT TYPE=”button” value=”Time on Page” onClick=”getLogoffTime()”>
This displays a pop up with the time spent on a page when a user leaves the
page .
Here is the script , first enter the following between the <HEAD> tags
<script>
// Copyright Chris Englmeier — machin@mindspring.com
// http://www.geocities.com/SiliconValley/Heights/2052
// You can use this script so long as this text remains
intact
<!–
var onHours = ” “;
var onMinutes = ” “;
var onSeconds = ” “;
var offHours = 0;
var offMinutes = 0;
var offSeconds = 0;
var logSeconds = 0;
var logMinutes = 0;
var logHours = 0;
var OnTimeValue = ” “;
var OffTimeValue = ” “;
var PageTimeValue = ” “;
// Back to previous page.
function WinOpen()
{
getLogoffTime();
}
// Loads HTML page to full window for View Source.
function WinOpen1()
{
alert(’\nPage will load to full screen.\n\nUse View/Document
Source from menu bar to view source.\n\nClose new window
to return to this page. ‘);
window.open(”js-timer.htm”,”DisplayWindow”,”menubar=yes,scrollbars=yes”);
window.open(”js-timer.htm”,”DisplayWindow”,”menubar=yes,scrollbars=yes”);
}
// Captures logon time.
function getLogonTime()
{
var now = new Date();
// Used to display logon time.
var ampm = (now.getHours() >= 12) ? ” P.M.” : ” A.M.”;
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? “:0″ : “:”) + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? “:0″ : “:”) + now.getSeconds();
// String to display log-on time.
OnTimeValue =(” ” + Hours + Minute + Seconds + ” ” + ampm);
// Capture logon time for use in timer().
onHours = now.getHours();
onMinutes = now.getMinutes();
onSeconds = now.getSeconds();
}
function getLogoffTime()
{
var now = new Date();
// Used to display logoff time.
var ampm = (now.getHours() >= 12) ? ” P.M.” : ” A.M.”;
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? “:0″ : “:”) + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? “:0″ : “:”) + now.getSeconds();
// String to display log-off time.
OffTimeValue =(” ” + Hours + Minutes + Seconds + ” ” + ampm);
// Capture logoff time for use in timer().
offHours = now.getHours();
offMinutes = now.getMinutes();
offSeconds = now.getSeconds();
timer();
}
// Compute difference between logoff time and logon time.
function timer()
{
if (offSeconds >= onSeconds)
{
logSeconds = offSeconds - onSeconds;
}
else
{
offMinutes -= 1;
logSeconds = (offSeconds + 60) - onSeconds;
}
if (offMinutes >= onMinutes)
{
logMinutes = offMinutes - onMinutes;
}
else
{
offHours -= 1;
logMinutes = (offMinutes + 60) - onMinutes;
}
logHours = offHours - onHours;
// Used to display time on page.
logHours = ((logHours < 10) ? “0″ : “:”) + logHours;
logMinutes = ((logMinutes < 10) ? “:0″ : “:”) + logMinutes;
logSeconds = ((logSeconds < 10) ? “:0″ : “:”) +logSeconds;
// String to display time on page
PageTimeValue =(” ” + logHours + logMinutes+ logSeconds);
displayTimes();
}
function displayTimes()
{
alert(”Thank you for visiting my HomePage\ \nLOG ON TIME : ” +OnTimeValue+”\n\nLOG OFF TIME : “+OffTimeValue+”\n\nTIME ON PAGE : ” + PageTimeValue);
}
// –>
</script>
now modify the <Body> tag like this
<body onUnLoad=”WinOpen()” onLoad=”getLogonTime()” ;>