Showing posts with label JavaScripts. Show all posts
Showing posts with label JavaScripts. Show all posts

Thursday, September 22, 2011

Highlight your source code in blog

[1] http://alexgorbatchev.com/SyntaxHighlighter/hosting.html
[2] http://alexgorbatchev.com/SyntaxHighlighter/download/
[3] How to add brush:cpp into your blog



 

Sunday, July 17, 2011

Why is JSON so popular? Developers want out of the syntax business.

[1] http://blog.mongolab.com/2011/03/why-is-json-so-popular-developers-want-out-of-the-syntax-business/

Monday, November 8, 2010

Decimal point in java script

[1] http://www.mredkj.com/javascript/nfbasic2.html

// Example: toFixed(2) when the number has no decimal places
// It will add trailing zeros
var num = 10;
var result = num.toFixed(2); // result will equal 10.00

// Example: toFixed(3) when the number has decimal places
// It will round to the thousandths place
num = 930.9805;
result = num.toFixed(3); // result will equal 930.981


PADDING '0'
-----------------------------------

function PadDigits(n, totalDigits)
{
n = n.toString();
var pd = '';
if (totalDigits > n.length)
{
for (i=0; i < (totalDigits-n.length); i++)
{
pd += '0';
}
}
return pd + n.toString();
}

Tuesday, November 17, 2009

FW: How to copy arrays and objects in Javascript

from: http://my.opera.com/GreyWyvern/blog/show.dml/1725165

How to copy arrays and objects in Javascript

Friday, 8. February 2008, 01:29:48

object, tip, clone, copy, array, javascript
There are a few things that trip people up with regards to Javascript. One is the fact that assigning a boolean or string to a variable makes a copy of that value, while assigning an array or an object to a variable makes a reference to the value. The trouble this causes can range from befuddlement - when two variables you assume are separate are in fact references to the same value - to frustration when you realize there is no native way to tell the Javascript engine to pass a value rather than a reference.

In PHP, for example, all assignments make copies unless you explicitly tell the engine to pass a reference using the =& operator. If only it were so simple in Javascript.

I'm pretty much reinventing the wheel with this post since this issue has been solved before, but the reason I'm writing it is because up until recently I had always been designing custom functions to copy objects of types I designed myself. Very efficient on a per-case basis, but not very reusable. There had to be a way to make such a thing work for any and all arrays and objects. So I searched the interwebs and was enlightened.

Firstly, arrays. Surprisingly, arrays are easy to copy because a couple of native Array object methods actually return a copy of the array. The easiest to use is the slice() method:

var foo = [1, 2, 3];
var bar = foo;
bar[1] = 5;
alert(foo[1]);
// alerts 5

var foo = [1, 2, 3];
var bar = foo.slice(0);
bar[1] = 5;
alert(foo[1]);
// alerts 2


The slice(0) method means, return a slice of the array from element 0 to the end. In other words, the entire array. Voila, a copy of the array. The only caveat to remember here is that this method works if the array contains only simple data types, like numbers, strings and booleans. If the array contains objects or other arrays (a multi-dimensional array), then those contained "objects" will be copied by reference, retaining a connection with the source array. In such a case you will need to copy the array as a full-fledged object.

Objects are trickier because there is no native method which returns a copy of the object. So instead we add one ourselves using a prototype method:

Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};


Notice the recursion going on; isn't it divine? :smile: The clone() method steps through the properties of any object one by one. If the property is itself an object or array, it calls the clone() method on that object too. If the property is anything else, it just takes the value of the property. Then the result received is assigned to a property of the same name in a new object.

Finally, after we're done stepping through all properties, return the new object which is a copy of - not a reference to - the old object. A call to the clone method is as simple as this:

var foo = {a: 1, b: 2, c: 3};
var bar = foo;
bar.b = 5;
alert(foo.b);
// alerts 5

var foo = {a: 1, b: 2, c: 3};
var bar = foo.clone();
bar.b = 5;
alert(foo.b);
// alerts 2

Monday, November 2, 2009

Forms' element samples (checkbox,radio,select,option)

[HTML>
[HEAD>
[TITLE>Checkbox Inspector[/TITLE>
[SCRIPT LANGUAGE="JavaScript">
function inspectBox() {
if (document.forms[0].checkThis.checked) {
alert("The box is checked.")
} else {
alert("The box is not checked at the moment.")
}
}


function inspectRadio() {
for (var i = 0; i [ document.forms[0].sex.length; i++) {
if (document.forms[0].sex[i].checked) {
break
}
}
alert("You chose " + document.forms[0].sex[i].value + ".")
}


function verifySong(entry) {
var song = entry.value
alert("Checking whether " + song + " is a Beatles tune...")
}


function showCarSelected() {
var list = document.forms[0].carSelect
alert("SelectIndex="+list.selectedIndex+" Value="+list.options[list.selectedIndex].value+" "+list.options[list.selectedIndex].text+" Car is selected.");
}


function checkAll(field)
{
for (i = 0; i [ field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i [ field.length; i++)
field[i].checked = false ;
}

[/SCRIPT>
[/HEAD>
[BODY>
[FORM>
[INPUT TYPE='checkbox' NAME='checkThis' onClick='javascript:inspectBox()'>Check here[BR>

[input type="radio" name="sex" value="male" onClick='javascript:inspectRadio()' > Male
[br />
[input type="radio" name="sex" value="female" onClick='javascript:inspectRadio()'> Female
[br>

[INPUT TYPE="text" NAME="song" VALUE = "Eleanor Rigby" onChange="verifySong(this)">[P>


[select name="carSelect" onChange="showCarSelected()" >
[option value="0" >Volvo[/option>
[option value="1" >Saab[/option>
[option >Mercedes[/option>
[option selected >Audi[/option>
[/select>


[/FORM>

[form name="myform" action="checkboxes.asp" method="post">

[b>Your Favorite Scripts & Languages[/b>[br>
[input type="checkbox" name="list" value="1">Java[br>
[input type="checkbox" name="list" value="2">Javascript[br>
[input type="checkbox" name="list" value="3">Active Server Pages[br>
[input type="checkbox" name="list" value="4">HTML[br>
[input type="checkbox" name="list" value="5">SQL[br>

[input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">
[input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.myform.list)">
[br>
[/FORM>
[/BODY>
[/HTML>

Tuesday, August 11, 2009

DecimalHexBinary data convertion

{HTML>
{HEAD>
{TITLE>Number Conversion Table{/TITLE>
{/HEAD>
{BODY>
{B>Using toString() to convert to other number bases:{/B>
{HR>
{TABLE BORDER=1>
{TR>
{TH>Decimal{/TH>{TH>Hexadecimal{/TH>{TH>Binary{/TH>{/TR>
{SCRIPT LANGUAGE="JavaScript">
var content = ""
for (var i = 0; i {= 20; i++) {
content += "{TR>"
content += "{TD>" + i.toString(10) + "{/TD>"
content += "{TD>" + i.toString(16) + "{/TD>"
//content += "{TD>" + i.toString(16).toUpperCase() + "{/TD>"
content += "{TD>" + i.toString(2) + "{/TD>{/TR>"
}
document.write(content)
{/SCRIPT>
{/TABLE>
{/BODY>
{/HTML>

Thursday, August 6, 2009

Multiple lines of string in JavaScript. Using 'back slash'

function createWarningPage()
{


content="";

content+="\
{CENTER>\
{a> {b>{SPAN CLASS='preppy'> Device Communication Time out after "+parent.left.timeoutduration+" Seconds . {/SPAN>{/b> {/a>\
{br>\
{a> Please try again. {/a>\
{BR>\
{BR>\
{BR>\
{BR>\
{/CENTER>";

document.write(content);

}

Friday, July 31, 2009

Timeout in JavaScript -- Sample

{head>

{script language="JavaScript1.2">

//configure flash (1000=1 second)
var speed=500

function flashit(){
var crosstable=document.getElementById? document.getElementById("spaexample") : document.all? document.all.spaexample : ""
if (crosstable){
if (crosstable.style.borderColor.indexOf("green")!=-1)//Start configure border colors//
crosstable.style.borderColor="red"
else
crosstable.style.borderColor="green" //End configure border colors//
}


//document.all.textcolumn.value=speed
mainform.textcolumn.value=speed



if (speed { 510)
{
speed++
setTimeout("flashit()",1000);
}

}


//setInterval("flashit()", speed)


{/script>
{/head>
{body>

{form name=mainform>

{table border="0" width="280" id="spaexample" style="border:5px solid green">
{tr>
{td>
Insert anything you want into this table.
{br>Insert anything you want into this table.
{br>Insert anything you want into this table.{br>{/td>
{/tr>
{/table>

{input type="text" name=textcolumn id="textcolumn" value=initvalue >

{script language="JavaScript1.2"> flashit() {/script>

{/form>
{/body>

Tuesday, June 23, 2009

JavaScripts-Examples

{TITLE>onStop Event Handler{/TITLE>
{SCRIPT LANGUAGE="JavaScript">
var counter = 0
var timerID
function startCounter() {
document.forms[0].display.value = ++counter;

if(document.forms[0].display.value >= 100){
haltCounter();
}
else{

timerID = setTimeout("startCounter()", 10);
}

}
function haltCounter() {

document.forms[0].display.value = "Time due! ";
clearTimeout(timerID);
counter = 0;

}
{!-- document.onstop = haltCounter; -->
{/SCRIPT>
{/HEAD>
{BODY>
{H1>onStop Event Handler{/H1>
{HR>
{P>Click the browser’s Stop button (in IE) to stop the script counter.{/P>
{FORM>
{P>{INPUT TYPE="text", NAME="display">{/P>
{INPUT TYPE="button", VALUE="Start Counter", onClick="startCounter()">
{INPUT TYPE="button", VALUE="Halt Counter", onClick="haltCounter()">
{/FORM>
{/BODY>
{/HTML>