This page has been robot translated, sorry for typos if any. Original content here.

Java Scripts Collection A collection of scripts


The following example can replace a script that controls the background color, text and links directly from the web page. The visitor of the page will only need to click on one of the cells here for such a small table:

__ __ __ __ __ __ __


At the heart of this method is the mouse click control command - span onClick = "..." that's the actual code itself (here code only one cell of the table):
<table style="CURSOR: hand" align=center bgcolor= ... >
<tbody> <tr> <td bgcolor=#C8CECD width="20" height="13">

<span onClick="bgColor=' #C8CECD '; fgColor=' #000000 '; linkColor=' #0000СС '; vlinkColor=' #9900СС '"> <font color="#000000">__</font>

</span> </td>

... </tr> </tbody>
</table>





This is the simplest script for setting a password on a web page. Paste into the <HEAD> tag. Here the password value is "name", but you can replace it with any other (uppercase letters are taken into account):
<code>&lt;script laguage=JavaScript&gt; &lt;br&gt; pass = prompt('Введите пароль');&lt;br&gt; if (pass=='name') { alert('Пароль верный') } &lt;br&gt; else { alert('Неверный пароль!'), top.location.href="/click?http://" }&lt;br&gt; &lt;/script&gt; &lt;br&gt;</code>




This script displays a greeting, a date and a day of the week, depending on the current time of day:
<code>&lt;script type=text/JavaScript&gt; function priv() { var dt = new Date(); var nt = dt.getHours(); var hs = ''; if ( nt&gt;=0 &amp;&amp; nt&lt;6 ) hs="Доброй ночи!"; if ( nt&gt;=6 &amp;&amp; nt&lt;11 ) hs="Доброе утро!"; if ( nt&gt;=11 &amp;&amp; nt&lt;18 ) hs="Добрый день!"; if ( nt&gt;=18 ) hs="Добрый вечер!"; return hs; } function dat() { var k = ''; var Day = ''; var Month = ''; var now = new Date(); var pr = now.getYear(); var yr = 2000; var mName = now.getMonth() + 1; var dName = now.getDay() + 1; var dayNr = ((now.getDate()&lt;10) ? "0" : "") + now.getDate(); pr = (pr % 100); if(pr==3) k="2003"; if(pr==4) k="2004"; if(pr==5) k="2005"; if(pr==6) k="2006"; if(pr==7) k="2007"; if(pr==8) k="2008"; if(pr==9) k="2009"; if(pr==10) k="2010"; if(pr==11) k="2011"; if(pr==12) k="2012"; if(pr==13) k="2013"; if(pr==14) k="2014"; if(dName==1) Day = "Воскресенье"; if(dName==2) Day = "Понедельник"; if(dName==3) Day = "Вторник"; if(dName==4) Day = "Среда"; if(dName==5) Day = "Четверг"; if(dName==6) Day = "Пятница"; if(dName==7) Day = "Суббота"; if(mName==1) Month="января"; if(mName==2) Month="февраля"; if(mName==3) Month="марта"; if(mName==4) Month="апреля"; if(mName==5) Month="мая"; if(mName==6) Month="июня"; if(mName==7) Month="июля"; if(mName==8) Month="августа"; if(mName==9) Month="сентября"; if(mName==10) Month="октября"; if(mName==11) Month="ноября"; if(mName==12) Month="декабря"; var nnn =(""+dayNr+" "+Month+" " + k + " года" + " - " + Day); document.open(); return nnn; } function ALZ(s) { return (s&lt;10?"0":"")+s.toString(); } function C2UT(s) { d=new Date(s); return ALZ(d.getDate())+"."+ALZ(d.getMonth()+1)+"."+d.getFullYear()+" " +ALZ(d.getHours())+":"+ALZ(d.getMinutes()); } &lt;/script&gt; &lt;script&gt;document.writeln(priv());&lt;/script&gt; &lt;script&gt;document.writeln(dat());&lt;/script&gt;</code>
Example:
| |





A simple script output date and running hours:
<code>&lt;Script laguage=JavaScript&gt; function fulltime() { var time=new Date(); document.clock.full.value=time.toLocaleString(); setTimeout('fulltime()',500) } &lt;/script&gt; &lt;form name=clock&gt; &lt;input type=text size=20 name=full&gt; &lt;/form&gt; &lt;script laguage=JavaScript&gt;fulltime(); &lt;/script&gt;</code>




A script that blocks a pop-up menu on the page when you right-click a mouse:
<code>&lt;SCRIPT&gt;function click() { if(event.button==2) {alert("..если хочешь - сохраняй всю страницу, а по частям не дам.. &lt;img src="/img/skin/smiles/smile.png" alt=":)"&gt;"); } } document.onmousedown=click; &lt;/SCRIPT&gt;</code>




To output the date and time of page editing, insert (before the script specify the font tag):
Example:

<code>&amp;lt;script language=JavaScript&amp;gt; d = new Date(); t = Date.parse(document.lastModified); r = Math.round((dt)/86400000); document.write("Сегодня "); document.write(d.toLocaleString()); &amp;lt;/script&amp;gt;</code>




You can kill the advertising banner of the Host from the People by simply adding the following code (after the </ html> tag) at the end of the page:
<noscript><noscript>
<plaintext><plaintext>





And here's another script that displays the title of your page in the bottom (status) panel of the browser in alphabetical order:
<code>&amp;lt;SCRIPT&amp;gt;&amp;lt;!-- var tit = document.title; var c = 0; function writetitle() {document.title = tit.substring(0,c); if(c==tit.length) {c = 0; setTimeout("writetitle()", 3000) } else{ c++; setTimeout("writetitle()", 200) }}writetitle() // --&amp;gt; &amp;lt;/SCRIPT&amp;gt;</code>




The following Java script is interesting in that it allows you to create a simple search on the pages of your site, even on a host that does not support Php, Perl, and similar dhtml scripts. The only inconvenience is that in the script itself you will have to enter data about the content of your pages in the following format: c ++; item [c] = new Array ("address.htm", "", "page name", "keywords", "description"); For pages not specified in the script, the search will not be carried out naturally. The script code must be inserted before the </ head> tag of your page: <code>&amp;lt;SCRIPT LANGUAGE="JavaScript"&amp;gt;&amp;lt;!-- var item = new Array(); // "имя","путь","название","ключевые,слова","описание страницы" c=0; item[c]=new Array("Y-net.narod.htm","","Введение","скачать, поиск, html, форум, веб, сайт, редактор, раскрутка, каталог","Редакторы, поиск, html"); c++; item[c]=new Array("y.htm","","название","слова","описание"); c++; item[c]=new Array("y.htm","","название","слова","описание"); c++; item[c]=new Array("y.htm","","название","слова","описание"); function search(frm) { win = window.open("","","width=500,height=400,resizable=yes,scrollbars=yes,status=1"); txt = frm.searchtext.value.split(" "); page="&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Результат поиска "+txt+"&amp;lt;/title&amp;gt;&amp;lt;META content='text/html; charset=windows-1251' http-equiv=Content-Type&amp;gt;&amp;lt;LINK href=\"/develop/js/styles.css\" rel=STYLESHEET type=text/css&amp;gt;\n&amp;lt;/head&amp;gt;\n&amp;lt;BODY link=#2288DD aLink=#ff0000 vLink=#ff0033 leftMargin=2 topMargin=0 MARGINHEIGHT=0 MARGINWIDTH=2&amp;gt;&amp;lt;center&amp;gt;&amp;lt;table border=0 cellspacing=10 width=80%&amp;gt;"; win.document.write(page); fnd = new Array(); total=0; for (i = 0; i &amp;lt; item.length; i++) { fnd[i] = 0; order = new Array(0, 4, 2, 3); for (j = 0; j &amp;lt; order.length; j++) for (k = 0; k &amp;lt; txt.length; k++) if (item[i][order[j]].toLowerCase().indexOf(txt[k]) &amp;gt; -1 &amp;&amp; txt[k] != "") fnd[i] += (j+1); } for (i = 0; i &amp;lt; fnd.length; i++) { n = 0; w = -1; for (j = 0;j &amp;lt; fnd.length; j++) if (fnd[j] &amp;gt; n) { n = fnd[j]; w = j; }; if (w &amp;gt; -1) total += show(w, win, n); fnd[w] = 0; } win.document.write("&amp;lt;/table&amp;gt;&amp;lt;br&amp;gt;&amp;lt;small&amp;gt;Всего найдено страниц : &amp;lt;b&amp;gt;"+total+"&amp;lt;/b&amp;gt; c словом &amp;lt;b&amp;gt;&amp;lt;font color=red&amp;gt;"+txt+"&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;"); win.document.close(); } function show(which,wind,num) { link = item[which][1] + item[which][0]; line = "&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;small&amp;gt;&amp;lt;a href='"+link+"'&amp;gt;"+item[which][2]+"&amp;lt;/a&amp;gt; Совпадений: "+num+"&amp;lt;br&amp;gt;"; line += item[which][4] + "&amp;lt;br&amp;gt;"+link+"&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;"; wind.document.write(line); return 1; } //--&amp;gt;&amp;lt;/script&amp;gt;</code>
The very form of the search is inserted into any place of the page with the following code:
<FORM method=get action="javascript:void(0)" onsubmit="search(this); return false;">
<input class=inputbox type=text name=searchtext value="html" size="30">
<input class=button type=submit value=" Поиск по сайту ">
</FORM>

The search result displays links and page names, and also how many times this word appears on each of the pages found. Here is an example of how this script works:
Example (forms only):





The following script considers the age of the site in days, you only need to change the date of creation of the site and the text of the output line:
<code>&lt;script language=JavaScript&gt;d0 = new Date('January 11, 2003'); d1 = new Date(); dt = (d1.getTime() - d0.getTime()) / (1000*60*60*24); document.write('&lt;SMALL&gt;'); document.write('Этому сайту исполнилось &lt;B&gt;' + Math.round(dt) + '&lt;/B&gt; дней.'); document.write('&lt;/SMALL&gt;'); &lt;/script&gt;</code>




The script of your banner network, in other words this script displays a random banner from the list specified in it. For each banner there are two parameters - the address of the link and the path to the gif image:

this [1] = "http: // link_01";
this [2] = "banner_01.gif";

<code>// &lt;!-- Main function banners() { this[1]="http://ссылка01"; this[2]="banner01.gif"; this[3]="http://ссылка02"; this[4]="banner02.gif"; this[5]="http://ссылка03"; this[6]="banner03.gif"; if ((navigator.appName == "Netscape") &amp;&amp; (parseInt(navigator.appVersion.substring(0,1)) &lt; 3)) { return(' '); } var j=(new Date()).getSeconds() % 3; document.write('&lt;a href=' + this[2*j+1] + ' target=blank&gt;&lt;img src=' + this[2*j+2] + ' border=0 width=88 height=31&gt;&lt;/a&gt;'); return(' '); } // --&gt;&lt;/SCRIPT&gt;&lt;SCRIPT language=JavaScript&gt;// &lt;!-- Main banners(); // --&gt;&lt;/SCRIPT&gt;</code>
Do not forget to also specify the number of your banners (here j = 3) in the following line: var j = (new Date ()). GetSeconds ()% 3;





This script is the original clock with a dial (example in the lower right corner):
<code>&lt;!-- fCol='444444'; //face colour. sCol='CC0000'; //seconds colour. mCol='444444'; //minutes colour. hCol='444444'; //hours colour. Ybase=28; //Clock height. Xbase=28; //Clock width. H='...'; H=H.split(''); M='....'; M=M.split(''); S='.....'; S=S.split(''); IE4=(document.all); Ypos=0; Xpos=0; dots=12; Split=360/dots; if (IE4){ document.write('&lt;div style="position:absolute;top:0px;left:0px"&gt;&lt;div style="position:relative"&gt;'); for (i=1; i &lt; dots+1; i++){ document.write('&lt;div id="ieDigits" style="position:absolute;top:0px;left:0px;width:30px;height:30px;font-family:Arial;font-size:10px;color:'+fCol+';text-align:center;padding-top:10px"&gt;'+i+'&lt;/code&gt;'); } document.write('&lt;/div&gt;&lt;/div&gt;') document.write('&lt;div style="position:absolute;top:0px;left:0px"&gt;&lt;div style="position:relative"&gt;'); for (i=0; i &lt; M.length; i++){ document.write('&lt;div id=y style="position:absolute;width:2px;height:2px;font-size:2px;background:'+mCol+'"&gt;&lt;/div&gt;'); } document.write('&lt;/div&gt;&lt;/div&gt;') document.write('&lt;div style="position:absolute;top:0px;left:0px"&gt;&lt;div style="position:relative"&gt;'); for (i=0; i &lt; H.length; i++){ document.write('&lt;div id=z style="position:absolute;width:2px;height:2px;font-size:2px;background:'+hCol+'"&gt;&lt;/div&gt;'); } document.write('&lt;/div&gt;&lt;/div&gt;') document.write('&lt;div style="position:absolute;top:0px;left:0px"&gt;&lt;div style="position:relative"&gt;'); for (i=0; i &lt; S.length; i++){ document.write('&lt;div id=x style="position:absolute;width:2px;height:2px;font-size:2px;background:'+sCol+'"&gt;&lt;/div&gt;'); } document.write('&lt;/div&gt;&lt;/div&gt;') } function clock(){ time = new Date (); secs = time.getSeconds(); sec = -1.57 + Math.PI * secs/30; mins = time.getMinutes(); min = -1.57 + Math.PI * mins/30; hr = time.getHours(); hrs = -1.57 + Math.PI * hr/6 + Math.PI*parseInt(time.getMinutes())/360; if (IE4){ Ypos=document.body.scrollTop+window.document.body.clientHeight-Ybase-10; Xpos=document.body.scrollLeft+window.document.body.clientWidth-Xbase-10; for (i=0; i &lt; dots; ++i){ ieDigits[i].style.pixelTop=Ypos-15+Ybase*Math.sin(-1.045 +i *Split*Math.PI/180) ieDigits[i].style.pixelLeft=Xpos-15+Xbase*Math.cos(-1.045 +i *Split*Math.PI/180) } for (i=0; i &lt; S.length; i++){ x[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(sec); x[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(sec); } for (i=0; i &lt; M.length; i++){ y[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(min); y[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(min); } for (i=0; i &lt; H.length; i++){ z[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(hrs); z[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(hrs); } } setTimeout('clock()',100); } clock(); //--&gt;&lt;/SCRIPT&gt;</code>
In the first 6 lines of the script you can change the color of any element and the size of the clock.
The output coordinates can be adjusted by changing two numbers in the last block of the script:
Ypos = document.body.scrollTop + window.document.body.clientHeight-Ybase-10;
Xpos = document.body.scrollLeft + window.document.body.clientWidth-Xbase-10;