|
Here are some useful free java scripts !!!!
1. This countdown script prints directly to the page, displaying the time left in minutes and seconds. The time duration can be set in the script. The timer will automatically stop when the time reaches 00 minutes and 00 seconds.
01 min and 17 sec
Countdown timer:
var sec = 30; // set the seconds
var min = 02; // set the minutes
function countDown() {
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
} else {
min = min;
}
if (sec<=9) { sec = "0" + sec; }
time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";
if (document.getElementById) { theTime.innerHTML = time; }
SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown();
});
===============================================
2. This script will allow you to limit the number of words and/or characters entered into a form. Easily customizable.
/* For additional information about this JavaScript
and how to use it, see the "Displaying Number of Words
Typed Into Form Fields" article, linked from the archives
at from http://willmaster.com/possibilities/archives/
The above note and the copyright line must remain with
this JavaScript source code. Comments below this point
in the code may be removed if desired.
// Customizing this JavaScript code requires specifying eight values.
// Value One:
// Specify the maximum number of characters the form field
// may contain. If you have no maximum, specify 0 (zero).
var MaximumCharacters = "80";
// Value Two:
// Specify the maximum number of words the form field may
// contain. If you have no maximum, specify 0 (zero).
var MaximumWords = "16";
// Value Three:
// Specify the form's name (provided by the name="_____"
// attribute in the FORM tag).
var FormName = "myForm";
// Value Four:
// Specify the name of the text field being monitored
// (provided by the name="_____" attribute in the
// INPUT or TEXTARE tag).
var TextFieldName = "TextField";
// Value Five:
// Specify the field name where where is to be displayed
// the number of characters the user has typed. Make
// it blank (nothing between the quotation marks) if
// you aren't displaying the number of characters typed.
var CharactersTypedFieldName = "CharsTyped";
// Value Six:
// Specify the field name where where is to be displayed
// the number of characters left that may be typed.
// Make it blank (nothing between the quotation marks)
// if you aren't displaying the number of characters
// left.
var CharactersLeftFieldName = "CharsLeft";
// Value Seven:
// Specify the field name where where is to be displayed
// the number of words the user has typed. Make it
// blank (nothing between the quotation marks) if you
// aren't displaying the number of words typed.
var WordsTypedFieldName = "WordsTyped";
// Value Eight:
// Specify the field name where where is to be displayed
// the number of words left that may be typed. Make it
// blank (nothing between the quotation marks) if you
// aren't displaying the number of words left.
var WordsLeftFieldName = "WordsLeft";
//////////////////////////////////////////////////////
// //
// No modfications are required below this point. //
// //
//////////////////////////////////////////////////////
var WordsMonitor = 0;
var MaxWords = parseInt(MaximumWords);
var MaxChars = parseInt(MaximumCharacters);
var textfield = 'document.' + FormName + '.' + TextFieldName + '.value';
function WordLengthCheck(s,l) {
WordsMonitor = 0;
var f = false;
var ts = new String();
for(var vi = 0; vi < s.length; vi++) {
vs = s.substr(vi,1);
if((vs >= 'A' && vs <= 'Z') || (vs >= 'a' && vs <= 'z') || (vs >= '0' && vs <= '9')) {
if(f == false) {
f = true;
WordsMonitor++;
if((l > 0) && (WordsMonitor > l)) {
s = s.substring(0,ts.length);
vi = s.length;
WordsMonitor--;
}
}
}
else { f = false; }
ts += vs;
}
return s;
} // function WordLengthCheck()
function CharLengthCheck(s,l) {
if(s.length > l) { s = s.substring(0,l); }
return s;
} // function CharLengthCheck()
function InputCharacterLengthCheck() {
if(MaxChars <= 0) { return; }
var currentstring = new String();
eval('currentstring = ' + textfield);
var currentlength = currentstring.length;
eval('currentstring = CharLengthCheck(' + textfield + ',' + MaxChars + ')');
if(CharactersLeftFieldName.length > 0) {
var left = 0;
eval('left = ' + MaxChars + ' - ' + textfield + '.length');
if(left < 0) { left = 0; }
eval('document.' + FormName + '.' + CharactersLeftFieldName + '.value = ' + left);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
if(CharactersTypedFieldName.length > 0) {
eval('document.' + FormName + '.' + CharactersTypedFieldName + '.value = ' + textfield + '.length');
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
} // function InputCharacterLengthCheck()
function InputWordLengthCheck() {
if(MaxWords <= 0) { return; }
var currentstring = new String();
eval('currentstring = ' + textfield);
var currentlength = currentstring.length;
eval('currentstring = WordLengthCheck(' + textfield + ',' + MaxWords + ')');
if (WordsLeftFieldName.length > 0) {
var left = MaxWords - WordsMonitor;
if(left < 0) { left = 0; }
eval('document.' + FormName + '.' + WordsLeftFieldName + '.value = ' + left);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
if (WordsTypedFieldName.length > 0) {
eval('document.' + FormName + '.' + WordsTypedFieldName + '.value = ' + WordsMonitor);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
} // function InputWordLengthCheck()
function InputLengthCheck() {
InputCharacterLengthCheck();
InputWordLengthCheck();
} // function InputLengthCheck()
===============================================
3. Sometimes you want a single hyperlink to lead to several pages and present a dynamic menu when clicked. This is usually done through an intermediary page. This script will provide a dynamic menu by creating an absolutely-positioned DIV tag on the fly, which will contain the list of links.
// Moves the div object to be directly beneath an object.
function move_box(an, box) {
var cleft = 0;
var ctop = 0;
var obj = an;
while (obj.offsetParent) {
cleft += obj.offsetLeft;
ctop += obj.offsetTop;
obj = obj.offsetParent;
}
box.style.left = cleft + 'px';
ctop += an.offsetHeight + 8;
// Handle Internet Explorer body margins,
// which affect normal document, but not
// absolute-positioned stuff.
if (document.body.currentStyle && document.body.currentStyle['marginTop']) {
ctop += parseInt(document.body.currentStyle['marginTop']);
}
box.style.top = ctop + 'px';
}
// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is current
function show_hide_multilink(an, width, height, className, links) {
var href = an.href;
var boxdiv = document.getElementById(href);
if (boxdiv != null) {
if (boxdiv.style.display=='none') {
// Show existing box, move it
// if document changed layout
move_box(an, boxdiv);
boxdiv.style.display='block';
}
else
// Hide currently shown box.
boxdiv.style.display='none';
return false;
}
// Create box object through DOM
boxdiv = document.createElement('div');
// Assign id equalling to the document it will show
boxdiv.setAttribute('id', href);
boxdiv.className = className;
boxdiv.style.position = 'absolute';
boxdiv.style.width = width + 'px';
boxdiv.style.height = height + 'px';
var linksarr = links.split(';');
var liobj;
var anchorobj;
for (var i = 0; i < linksarr.length; i = i + 2) {
liobj = document.createElement('li');
liobj.className = className;
anchorobj = document.createElement('a');
anchorobj.classname=className;
anchorobj.href = linksarr[i];
anchorobj.target = '_top';
anchorobj.appendChild(document.createTextNode(linksarr[i + 1]));
liobj.appendChild(anchorobj);
boxdiv.appendChild(liobj);
}
document.body.appendChild(boxdiv);
move_box(an, boxdiv);
// The script has successfully shown the box,
// prevent hyperlink navigation.
return false;
}
========================================
4. Your visitors may not be familiar with style sheets for printing and don't see a 'print preview' in their browser. This script will provide them with a reliable print preview.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent( function(){ add_print_link( 'nav' ) } );
function add_print_link( id ){
if( !document.getElementById ||
!document.getElementById( id ) ) return;
// add extra functions to page tools list
var print_page = document.getElementById( id );
// create print link
var print_function = document.createElement('p');
print_function.className = 'print-link';
print_function.onclick = function(){ print_preview(); return false; };
print_function.appendChild( document.createTextNode( 'Print the Page' ) );
}
function print_preview() {
// Switch the stylesheet
setActiveStyleSheet('Print Preview');
// Create preview message
add_preview_message();
// Print the page
window.print();
}
function add_preview_message(){
var main_content = document.getElementById('content');
var main_body = main_content.parentNode;
if (document.getElementById){
var preview_message = document.createElement('div');
preview_message.id = 'preview-message';
// Create Heading
var preview_header = document.createElement('h3');
var preview_header_text = document.createTextNode('This is a print preview of this page');
preview_header.appendChild(preview_header_text);
// Create paragraph
var preview_para = document.createElement('p');
var preview_para_text = document.createTextNode('Without this message of course. ');
var cancel_function_link = document.createElement('a');
cancel_function_link.onclick = function(){ cancel_print_preview(); return false; };
cancel_function_link.setAttribute('href', '#');
var cancel_function_link_text = document.createTextNode('Return to the existing page.');
cancel_function_link.appendChild(cancel_function_link_text);
preview_para.appendChild(preview_para_text); //
preview_para.appendChild(cancel_function_link);
// Put it all toegether
preview_message.appendChild(preview_header);
preview_message.appendChild(preview_para);
main_body.insertBefore(preview_message, main_content);
}
}
function cancel_print_preview() {
// Destroy the preview message
var print_preview = document.getElementById('preview-message');
var main_body = print_preview.parentNode;
main_body.removeChild(print_preview);
// Switch back stylesheet
setActiveStyleSheet('default');
}
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}
==============================================
More Coming !
|