beautiful sky

Current Location: Home->Tutorials->Javascript->Browser Functions

An introduction to math using javascript.

Current Location: Home->Tutorials->Javascript->Math

This page includes examples and code on how to:

Convert character codes into ascii, Convert ascii codes into character, Add subtract and get the length of variables, Convert Hexidecimal colors to RGB, Convert RGB colors to Hexidecimal.
 

Javascript to get the ascii code of a specific character:



Code:
function asccon()
{
var incar = document.getElementById('incc').value.substring(0,1);
var outcar = incar.charCodeAt(0);
alert("ASCII("+outcar+")");
}
 

Javascript to get the character of a specific ascii code:



Code:
function chrcon()
{
var incar = String.fromCharCode(document.getElementById('inaa').value);
alert("chr("+incar+")");
}
 

Simple start on adding and incramenting in javascript:

To create a variable with the value of 2000 and add 1 to it:
var abc1 = 2000+1;  //(abc1 is now set to 2001)/

To incrament the same variable again:
abc1 = abc1*1+1;  //(abc1 is now set to 2002)/use *1 to ensure you get the numeric value of a variable

To define abc1 as a string variable:
abc1 = abc1+'';  //(abc1 is now set to '2002')/use +'' to ensure you get the string value of a variable

To find out the length of a string variable (.length returns the number of characters in a string):
abc1 = abc1.length;  //(abc1 is now set to 4)

Another way to incrament numeric variables:
abc1++;  //(abc1 is now set to 5)

To add a string to the end of a variable:
var text1 = 'hello my ';
text1 = text1+'name is mike'; // (text1 is now set to 'hello my name is mike')
 

Convert Hexidecimal colors to RGB:







CODE:
coly = "#9999FF"
function getcol(coly)
{
if(coly.substr(0,1)=='#'){coly=coly.substr(1,6);}
var r = parseInt(coly.substring(0,2),16);
var g = parseInt(coly.substring(2,4),16);
var b = parseInt(coly.substring(4,6),16);
document.getElementById('outr').value= r;
document.getElementById('outg').value= g;
document.getElementById('outb').value= b;
}
 

Convert RGB colors to Hexidecimal:







CODE:
colr = "153", colg = "153", colb = "255"

var nposib = new Array(256);
var hexs = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
var hbh = 0;
for(i=0;i<16;i++){
for(j=0;j<16;j++){
nposib[hbh] = hexs[i] + hexs[j];
hbh++;
}
}
function getcolb(colr,colg,colb)
{
document.getElementById('outhex').value="#"+nposib[colr]+nposib[colg]+nposib[colb];
document.getElementById('inr').style.backgroundColor=document.getElementById('outhex').value;
document.getElementById('ing').style.backgroundColor=document.getElementById('outhex').value;
document.getElementById('inb').style.backgroundColor=document.getElementById('outhex').value;
setTimeout("clearcol();",650)
}
 

Main Links

Design Tools

Javascript Tutorials

CSS Tutorials

Design Tutorials

Links