Archive - Javascript

"They can because they think they can."  - Virgil 
 



.Contents.

bar1
Copy
Move
Delete
Rename

Message Popup
Cycle through list
The Script Vault  1,163 script





bar1
  



[Download Notepad++]






bar1
    


Fading Function

Script Code

<script type="text/javascript">
window.onload = FadeFunction
var i = 0
function FadeFunction() {
if (i < 350) {
i=i+3
}
var d = document.body.style
d.filter = "alpha(Opacity="+i+")"
d.opacity = i/350
var num = document.all ? 5 : 350
setTimeout("FadeFunction()",num)
}

</script>





Blinking Text

Script Code
<script type="text/javascript">

function done(){
var el=document.getElementById('blinker');

// IE has problems with setAttribute on 'style'
// el.setAttribute('style','color: #f00; text-decoration: none;');
// set directly
el.style.color='#f00';
el.style.textDecoration='none';
el.firstChild.nodeValue="DO NOT RESTART COMPUTER Installing Office 2010";
var t=setTimeout("countdown()",3800); //3.8 seconds
}

function countdown(){
var el=document.getElementById('blinker');
el.style.color='#88f';
el.style.textDecoration='blink';
var t=setTimeout("done()",3800); //3.8 seconds
}

// attach event after page loads
if( window.addEventListener ) {
window.addEventListener('load',countdown,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('load',countdown,false); //proper
} else if( window.attachEvent ) {
//window.attachEvent("onload", countdown); //IE only
}

</script>






Suppress Key
Input

Script Code
<script type="text/javascript">
function SupressRefresh(){
if (event.keyCode==116){event.returnValue=false} //116 is F5
}
document.onkeydown = SupressRefresh
</script>




Gradient Color Change

Script Code
<script type="text/javascript">

//top.resizeTo(300,200);
//top.moveTo(0,0);


// Select fade-effect below:
// Set 1 if the background may fade from dark to medium
// Set 2 if the background may fade from light to medium
// Set 3 if the background may fade from very dark to very light light
// Set 4 if the background may fade from light to very light
// Set 5 if the background may fade from dark to very dark
var fade_effect=2

// What type of gradient should be applied Internet Explorer 5x or higher?
// Set "none" or "horizontal" or "vertical"
var gradient_effect="vertical"

// Speed higher=slower
var speed=40

///////////////////////////////////////////////////////////////////////////
// CONFIGURATION ENDS HERE
///////////////////////////////////////////////////////////////////////////

var browserinfos=navigator.userAgent
var ie4=document.all&&!document.getElementById
var ie5=document.all&&document.getElementById&&!browserinfos.match(/Opera/)
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var opera=browserinfos.match(/Opera/)
var browserok=ie4||ie5||ns4||ns6||opera

if (fade_effect==1) {
var darkmax=1
var lightmax=127
}
if (fade_effect==2) {
var darkmax=127
var lightmax=254
}
if (fade_effect==3) {
var darkmax=1
var lightmax=254
}
if (fade_effect==4) {
var darkmax=190
var lightmax=254
}
if (fade_effect==5) {
var darkmax=1
var lightmax=80
}
var hexc = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F')

var newred
var newgreen
var newblue
var oldred
var oldgreen
var oldblue

var redcol_1
var redcol_2
var greencol_1
var greencol_2
var bluecol_1
var bluecol_2
var oldcolor
var newcolor
var firsttime=true

var stepred=1
var stepgreen=1
var stepblue=1

function setrandomcolor() {
var range=(lightmax-darkmax)
if (firsttime) {
newred=Math.ceil(range*Math.random())+darkmax
newgreen=Math.ceil(range*Math.random())+darkmax
newblue=Math.ceil(range*Math.random())+darkmax
firsttime=false
}

oldred=Math.ceil(range*Math.random())+darkmax
oldgreen=Math.ceil(range*Math.random())+darkmax
oldblue=Math.ceil(range*Math.random())+darkmax

stepred=newred-oldred
if (oldred>newred) {stepred=1}
else if (oldred<newred) {stepred=-1}
else {stepred=0}

stepgreen=newgreen-oldgreen
if (oldgreen>newgreen) {stepgreen=1}
else if (oldgreen<newgreen) {stepgreen=-1}
else {stepgreen=0}

stepblue=newblue-oldblue
if (oldblue>newblue) {stepblue=1}
else if (oldblue<newblue) {stepblue=-1}
else {stepblue=0}
fadebg()
}

function fadebg() {
if (newred==oldred) {stepred=0}
if (newgreen==oldgreen) {stepgreen=0}
if (newblue==oldblue) {stepblue=0}
newred+=stepred
newgreen+=stepgreen
newblue+=stepblue

if (stepred!=0 || stepgreen!=0 || stepblue!=0) {
redcol_1 = hexc[Math.floor(newred/16)];
redcol_2 = hexc[newred%16];
greencol_1 = hexc[Math.floor(newgreen/16)];
greencol_2 = hexc[newgreen%16];
bluecol_1 = hexc[Math.floor(newblue/16)];
bluecol_2 = hexc[newblue%16];
newcolor="#"+redcol_1+redcol_2+greencol_1+greencol_2+bluecol_1+bluecol_2
if (ie5 && gradient_effect!="none") {
if (gradient_effect=="horizontal") {gradient_effect=1}
if (gradient_effect=="vertical") {gradient_effect=0}
greencol_1 = hexc[Math.floor(newred/16)];
greencol_2 = hexc[newred%16];
bluecol_1 = hexc[Math.floor(newgreen/16)];
bluecol_2 = hexc[newgreen%16];
redcol_1 = hexc[Math.floor(newblue/16)];
redcol_2 = hexc[newblue%16];
var newcolorCompl="#"+redcol_1+redcol_2+greencol_1+greencol_2+bluecol_1+bluecol_2
document.body.style.filter=
"progid:DXImageTransform.Microsoft.Gradient(startColorstr="+newcolorCompl+", endColorstr="+newcolor+" GradientType="+gradient_effect+")"
}
else {
document.bgColor=newcolor
}
var timer=setTimeout("fadebg()",speed);
}
else {
clearTimeout(timer)
newred=oldred
newgreen=oldgreen
newblue=oldblue
oldcolor=newcolor
setrandomcolor()
}
}

if (browserok) {
window.onload=setrandomcolor
}
</script>





Alert

Description
This method appends a name to a given path. A delimiter is added automatically. The following script will pop up the alert window below:

Script Code
<SCRIPT LANGUAGE="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");|
var newpath = fso.BuildPath("c:
\tmp%22,%20%22Test.txt\\tmp", "Test.txt");
alert(newpath);
-->
</SCRIPT>




Assign a Variable (from a text file)

Description
This method appends a name to a given path. A delimiter is added automatically. The following script will pop up the alert window below:

Script Code
<SCRIPT TYPE="text/javascript">
var txtFile = new XMLHttpRequest();
if(window.XMLHttpRequest) {
var req = new XMLHttpRequest();
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
//alert("Permission could be denied for cross-browser scripting.");
//manual support for Chrome Browser - until I can figure out something else
lines = "some default text goes here";
}
} else if(window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP");
}

txtFile.open("GET", "http://eddiejackson.net/data/dailybrief.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;

lines = "Text from your file will be placed here&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + txtFile.responseText; // Will separate each line into an array
}
}

}

txtFile.send(null);

</script>






Background Color Changer

Script

<script language="JavaScript">
<!--
//you can assign the initial color of the background here
r=255;
g=255;
b=255;
flag=0;
t=new Array;
o=new Array;
d=new Array;

function hex(a,c)
{
t[a]=Math.floor(c/16)
o[a]=c%16
switch (t[a])
{
case 10:
t[a]='A';
break;
case 11:
t[a]='B';
break;
case 12:
t[a]='C';
break;
case 13:
t[a]='D';
break;
case 14:
t[a]='E';
break;
case 15:
t[a]='F';
break;
default:
break;
}
switch (o[a])
{
case 10:
o[a]='A';
break;
case 11:
o[a]='B';
break;
case 12:
o[a]='C';
break;
case 13:
o[a]='D';
break;
case 14:
o[a]='E';
break;
case 15:
o[a]='F';
break;
default:
break;
}
}

function ran(a,c)
{
if ((Math.random()>2/3||c==0)&&c<255)
{
c++
d[a]=2;
}
else
{
if ((Math.random()<=1/2||c==255)&&c>0)
{
c--
d[a]=1;
}
else d[a]=0;
}
return c
}
function do_it(a,c)
{
if ((d[a]==2&&c<255)||c==0)
{
c++
d[a]=2
}
else
if ((d[a]==1&&c>0)||c==255)
{
c--;
d[a]=1;
}
if (a==3)
{
if (d[1]==0&&d[2]==0&&d[3]==0)
flag=1
}
return c
}
function disco()
{
if (flag==0)
{
r=ran(1, r);
g=ran(2, g);
b=ran(3, b);
hex(1,r)
hex(2,g)
hex(3,b)
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
flag=50
}
else
{
r=do_it(1, r)
g=do_it(2,g)
b=do_it(3,b)
hex(1,r)
hex(2,g)
hex(3,b)
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
flag--
}

setTimeout('disco()',50)
}
//-->
</script>





Cycling Through List

Script Code
<script language="Javascript">
<!--
var myFileSysObj = new ActiveXObject("Scripting.FileSystemObject");
var myInputTextStream = myFileSysObj.OpenTextFile("computer.txt", 1, true);
while(!myInputTextStream.AtEndOfStream){
alert(myInputTextStream.ReadLine();
}
myInputTextStream.Close();

-->
</script>




Copy

Description
This method copies a source file to a destination file. If the destination file exists, it is overwritten only when overwriteFlag is true. On some systems, the destination file cannot be overwritten no matter what. Here is a script that copies test.txt from f:\ to f:\EddieJackson:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

var newpath = fso.CopyFile("f:\\test.txt", "f:\\EddieJackson\\test.txt");

-->

</SCRIPT>





Copy Folder


Description

This method copies a source folder to a destination folder. If the destination folder exists, it is overwritten only when overwriteFlag is true. On some systems, the destination folder cannot be overwritten no matter what. Here is a script that copies temp from f:\ to f:\EddieJackson:

Script Code
<SCRIPT LANGUAGE="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFolder("f:\\temp", "f:\\EddieJackson
\\temp");
-->
</SCRIPT>

 




Create Folder


Description

This method creates a folder
called folderName. It returns the folder name. Here is a script that creates a folder new:

Script Code
<SCRIPT LANGUAGE="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFolderName = fso.CreateFolder("f:
\\new");
-->

</SCRIPT>






Create File

Description
This method creates a text file called fileName. To overwrite an existing file, you need to include the optional parameter overwriteFlag and set it to true. If you don't include it, or if you set it to false, you will get the "File already exists" error message. This method returns the file object. Here is a script that creates a file new.txt, overwriting any existing file with the same name:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

var newFileObject = fso.CreateTextFile("d:\\new.txt", true);

-->

</SCRIPT>





Delete File

Description
This method deletes a file called fileName. To delete a file with a read permission only, you need to include the optional parameter readPermissionFlag and set it to true. If you don't include it, or if you set it to false, you will get the "Permission denied" error message. Here is a script that deletes the file "new.txt", even if it has only read permission:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

fso.DeleteFile("d:\\new.txt", true);

-->

</SCRIPT>




Delete Folder

Description
This method deletes a folder called folderName. To delete a folder with a read permission only, you need to include the optional parameter readPermissionFlag and set it to true. If you don't include it, or if you set it to false, you will get the "Permission denied" error message. On some systems, the optional parameter readPermissionFlag does not have any effect, and the folder is deleted no matter what. Here is a script that deletes the folder "new", only if it has a write permission:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

fso.DeleteFolder("d:\\new", false);

-->

</SCRIPT>




Rename a File

Script Code
function ChangeFileName()
{
var fso, f;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile("FilePath/MyFile.txt");
f.name = "MyFile.htm";
}




Check for Driver Letter

Description
This method checks whether a disk drive with the given letterDrive exists. It returns true if it exists, and false otherwise. Here is a script that checks if your PC has a "d" drive:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

hasDriveBool = fso.DriveExists("d");

-->

</SCRIPT>






Check If File Exists

Description
This method checks whether a file fileName exists. It returns true if it exists, and false otherwise. Here is a script that checks if file old.txt exists:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

fileBool = fso.FileExists("d:\\old.txt");

-->

</SCRIPT>





Check If Folder Exists

Description
This method checks whether a folder folderName exists. It returns true if it exists, and false otherwise. Here is a script that checks if folder old exists:

<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

folderBool = fso.FolderExists("d:\\old");

-->

</SCRIPT>






Return Only File Name

Description
This method returns a string containing the base name of pathName, without any extension or path. The use of this method is in applications that need to assemble file names on the basis of another file. For example, an application can name its output file using the base name of the executable (myapp.exe) and a different extenstion (myapp.out). The following script should pop up an alert box with the string myapp:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

baseName = fso.GetBaseName("f:\\EddieJackson\\js\\tips\\myapp.exe");

-->

</SCRIPT>





Return File Extension

Description
This method returns the file extension name of the given path. You can use this method to determine file types, according to their extensions. The following script should pop up an alert box with the string "exe":

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

extName = fso.GetExtensionName("f:\\EddieJackson\\js\\tips\\myapp.exe");

alert(extName);

-->

</SCRIPT>





Return File Name

Description

This method returns the file name of the given path. Use this method to assemble new file names on the basis of the given file. The following script should pop up an alert box with the string "myapp.exe":

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

fileName = fso.GetFileName("f:\\EddieJackson\\js\\tips\\myapp.exe");

alert(fileName);

-->

</SCRIPT>



Return Parent Folder

This method returns the parent folder name of the given file or folder. A full path of the parent folder is returned. The following script should pop up an alert box with the string "f:\EddieJackson\js\tips":

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

folderName =

  fso.GetParentFolderName("f:\\EddieJackson\\js\\tips\\myapp.exe");

alert(folderName);

-->

</SCRIPT>





Return Special Folder

Description

This method returns one of three special folders: windows folder, system folder, or temporary folder, according to the given folderCode. The given folderCode is either 0 for a windows folder, 1 for a system folder, or 2 for a temporary folder. A full path is returned. On a typical installation, "c:\windows" is returned as the windows folder, "c:\windows\system" is returned as the system folder, and "c:\windows\temp" is returned as the temporary folder. The following script should pop up an alert box with the string "c:\windows":

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

winFolder = fso.GetSpecialFolder(0);

alert(winFolder);

-->

</SCRIPT>






Create Unique Names

Description
This method returns a randomly-generated temporary file or folder. On a Windows installation, this file may start with "rad", followed by a random sequence of five alphanumeric characters. Use this method in applications where you need to create temporary files with unique names. The following script should pop up an alert box with a string that looks like "radEF98E.tmp":

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

tmpName = fso.GetTempName();

alert(tmpName);

-->

</SCRIPT>




Move File

Description
This method moves a source file to a destination file. If the destination file exists, the file will not be moved and you'll get the "File already exists" error message. Here is a script that moves test.txt from d:\ to d:\EddieJackson:

MoveFile(source, destination)

Undefined

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

var newpath = fso.CopyFile("d:\\test.txt", "d:\\EddieJackson\\test.txt");

-->

</SCRIPT>





Move Folder

Description
This method moves a source folder to a destination folder. If the destination folder exists, the folder will not be moved and you'll get the "File already exists" error message. Here is a script that moves new from d:\ to d:\EddieJackson:

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

var newpath = fso.

File("d:\\new", "d:\\EddieJackson\\new");

-->

</SCRIPT>




Open Text Stream

Description
This method opens a text stream object to the specified file, fileName, for reading and writing. The second parameter, iomode, indicates the mode of the file opening. Possible values are:

Constant

Value

Description

ForReading

1

Opens the file for reading

ForWriting

2

Opens the file for writing

ForAppending

8

Opens the file for appending


You need to set the constants yourself. JavaScript does not support them by itself. The third parameter, create, is a Boolean value indicating whether to create the file if does not exist (true) or to issue an error message if the file does not exist (false). The last parameter, format, is optional and indicates the file type. If not specified, the default file type is ASCII. The possible values of format are:

Constant

Value

Description

TristateUseDefault

-2

Opens the file using the system default

TristateTrue

-1

Opens the file as Unicode

TristateFalse

0

Opens the file as ASCII

You need to set these constants yourself. JavaScript does not set them by itself. Here is a script that opens the file f:\EddieJackson\js\tips\001122.html for writing as an ASCII. It creates the file if it does not already exist:


Script Code

<SCRIPT LANGUAGE="JavaScript">

<!--

var ForWriting = 2;

var TriStateFalse = 0;

var fso = new ActiveXObject("Scripting.FileSystemObject");

var newFile = fso.OpenTextFile("f:\\EddieJackson\\js\\tips\\001122.html",  ForWriting, true, TriStateFalse);

-->

</SCRIPT>






Write to File

Script Code

<script type="text/javascript">

function WriteToFile() {
try {
var fso, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
s = fso.OpenTextFile("computer1.txt" , 8, 1, -2);
s.writeline("This is a test");
s.Close();
}
catch(err){
var strErr = 'Error:';
strErr += '\nNumber:' + err.number;
strErr += '\nDescription:' + err.description;
document.write(strErr);
}
}
WriteToFile();
</script>







The file Object

Return File Object

Description
This method returns the file object of the given file name. The following script should create a file object for f:\EddieJackson\js\tips\myapps.txt:

GetFile(pathName)

Object

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

fileObj = fso.GetFile("f:\\EddieJackson\\js\\tips\\myapp.txt");

-->

</SCRIPT>


Here are the file object's properties:

Property

Description

Attributes

Refers to the file attributes

DateCreated

Returns the file's creation date

DateLastAccessed

Returns the file's last-accessed date

DateLastModified

Returns the file's last-modified date

Drive

Returns the file's letter drive

Name

Returns the file name

ParentFolder

Returns the file's parent folder name

Path

Returns the file's long path

ShortPath

Returns the file's short path

Size

Returns the file size

Type

Returns the file type

 

Here are the file object's methods:

Method

Description

Copy()

Copies the file from one folder to another

Delete()

Removes the file

Move()

Moves the file from one folder to another

OpenAsTextStream()

Opens the file as a text stream







The Folder Object

Return Folder Object

Description
This method returns the folder object of the given folder name. The following script should create a folder object for f:\EddieJackson\js\tips:

GetFolder(pathName)

Object

Script Code
<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

folderObj = fso.GetFolder("f:\\EddieJackson\\js\\tips");

-->

</SCRIPT>


Here are the folder object's properties:

Property

Description

Attributes

Refers to the folder attributes

DateCreated

Returns the folder's creation date

DateLastAccessed

Returns the folder's last-accessed date

DateLastModified

Returns the folder's last-modified date

Drive

Returns the folder's letter drive

Files

Returns the folder's files collection, containing all the file objects in the folder

IsRootFolder

Returns true if the folder is the root folder, false otherwise

Name

Returns the folder name

ParentFolder

Returns the folder's parent folder name

Path

Returns the folder's long path

ShortName

Returns the folder's short name

ShortPath

Returns the folder's short path

Size

Returns the folder size

SubFolder

Returns a folder's collection of all folders contained in the folder

Type

Returns the folder type

Here are the folder object's methods:

Method

Description

Copy()

Copies the folder from one folder to another

Delete()

Removes the folder

Move()

Moves the folder from one folder to another







The Drive Object

Return Drive Object

Description
This method returns the drive object of the given drive name. The following script should create a drive object for f:

Drives

Collection

The drives collection holds a read-only collection of all available drives in the system. It has two properties:

Property

Description

Count

Returns the number of items in the collection

Item

Sets or returns an item from the collection that matches a specified key

DriveType

Returns the drive type


 

GetDrive(letterDrive)

Object


Script Code

<SCRIPT LANGUAGE="JavaScript">

<!--

var fso = new ActiveXObject("Scripting.FileSystemObject");

driveObj = fso.GetDrive("f");

-->

</SCRIPT>

The drive object does not have any methods. Here are the drive object's properties:

Property

Description

AvailableSpace

Returns the number of free bytes on the given drive

DriveLetter

Returns the drive letter of a physical local drive or a shared network

DriveType

Returns the drive type

FileSystem

Returns the file system type for the specified drive

FreeSpace

Returns the number of free bytes on the given drive

IsReady

Returns the status of the drive

Path

Returns the path of the given drive

RootFolder

Returns the folder object of the root folder

SerialNumber

Returns the unique serial number of the volume

ShareName

Returns the shared name of a network drive

TotalSize

Returns the total number of bytes on a specified drive

VolumeName

Sets or returns the volume name of the specified drive

 

 

 

 

 














 

 


 












































































  •   About

      
    I'm a Computer
      
    Systems Engineer

      
    Living and loving life
    ........................................


     
    Author

    ....





    ..