Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
Monday, November 4, 2013
Published 1:18 AM by Google Map with 0 comment
Friday, December 28, 2012
Published 8:37 AM by Aruna Jayathilaka with 0 comment
Try Catch (Exception)
try { } catch(err) { } finally { }
Try Catch (Exception)
Javascript Error Handling
Try CatchExample
try
{
//lines of codes
}
catch(err)
{
//err.description
//err.message
//err.name
//err.number
//err.stack
}
finally
{
//success or failure , code executes here
}
{
//lines of codes
}
catch(err)
{
//err.description
//err.message
//err.name
//err.number
//err.stack
}
finally
{
//success or failure , code executes here
}
Throw
Example
function ThrowHelper()
{
var err = new Error(123,"Error in helper");
throw err;
}
//elsewhere in code
try
{
ThrowHelper();
}
catch(ex)
{
var msg = ex.number +" :"+ex.message;
//log msg
}
{
var err = new Error(123,"Error in helper");
throw err;
}
//elsewhere in code
try
{
ThrowHelper();
}
catch(ex)
{
var msg = ex.number +" :"+ex.message;
//log msg
}
Published 7:02 AM by Aruna Jayathilaka with 1 comment
Javascript Array Functions (Array)
var fruit = ["apple","orange","banana"];
log(fruit.sort());//apple,banana,orange
fruit.push("pear");
log(fruit.sort());//apple,banana,orange,pear
var vegitables = ["carrot","broccoli","cauliflovd"];
var all = fruit.concat(vegitables);
log(all);//apple,orange,banana,carrot,broccoli,cauliflovd
Javascript Array Functions (Array)
Javascript Array functions
push,pop,concat,map,filter,some,every,forEach,reduce,sort,splice,slice,join,reverseArray is a Stack data sructure (first in first out)
Example
var fruit = ["apple","orange","banana"];
log(fruit.sort());//apple,banana,orange
fruit.push("pear");
log(fruit.sort());//apple,banana,orange,pear
var vegitables = ["carrot","broccoli","cauliflovd"];
var all = fruit.concat(vegitables);
log(all);//apple,orange,banana,carrot,broccoli,cauliflovd
var firt = fruit.slice(0,1);
log(first);//apple
var result = fruit.splice(1,2,"melon","grape");
log(result);//start in 1 no of 2 remove and add "melon","grape"
//out put is orange,banana
//splice is immutable function in javascript
fruit.splice(1,2,"melon","grape");
log(fruit);
//apple,melon,grape
fruit = fruit.map(function(i){return i.toUpperCase()});
log(fruit);
//APPLE,MELON,GRAPE
fruit = fruit.map(function(i){return {fruitname:i}});
log(fruit);
//[object Object],[object Object],[object Object]
fruit = fruit.filter(function(){
return i[0]==="a";
});
log(fruit);
//apple
fruit = fruit.every(function(){
return i[0]==="a";
});
log(fruit);
//false //if every items start with "a"
fruit = fruit.every(function(){
return i.length>0;
});
log(fruit);
//true //if every items has more than 0 length
fruit = fruit.some(function(){
return i.length>0;
});
log(fruit);
//true //if some items has more than 0 length
fruit.forEach(function(){
log(i);
});
log(fruit.sort());//apple,banana,orange
fruit.push("pear");
log(fruit.sort());//apple,banana,orange,pear
var vegitables = ["carrot","broccoli","cauliflovd"];
var all = fruit.concat(vegitables);
log(all);//apple,orange,banana,carrot,broccoli,cauliflovd
var firt = fruit.slice(0,1);
log(first);//apple
var result = fruit.splice(1,2,"melon","grape");
log(result);//start in 1 no of 2 remove and add "melon","grape"
//out put is orange,banana
//splice is immutable function in javascript
fruit.splice(1,2,"melon","grape");
log(fruit);
//apple,melon,grape
fruit = fruit.map(function(i){return i.toUpperCase()});
log(fruit);
//APPLE,MELON,GRAPE
fruit = fruit.map(function(i){return {fruitname:i}});
log(fruit);
//[object Object],[object Object],[object Object]
fruit = fruit.filter(function(){
return i[0]==="a";
});
log(fruit);
//apple
fruit = fruit.every(function(){
return i[0]==="a";
});
log(fruit);
//false //if every items start with "a"
fruit = fruit.every(function(){
return i.length>0;
});
log(fruit);
//true //if every items has more than 0 length
fruit = fruit.some(function(){
return i.length>0;
});
log(fruit);
//true //if some items has more than 0 length
fruit.forEach(function(){
log(i);
});
Published 4:46 AM by Aruna Jayathilaka with 0 comment
Javascript Function
Javascript function definitions,There are few different methods to define a function in javascript
Example
function Samsple1()
{
log('Hello Aruna');
}...
Javascript Function
Javascript function definitions
There are few different methods to define a function in javascriptExample
function Samsple1()
{
log('Hello Aruna');
}
{
log('Hello Aruna');
}
Example
var fun = function Samsple2()
{
log('Hello Aruna');
};
{
log('Hello Aruna');
};
Example
var ops = {
Add:function AddNumbers(n1,n2)
{
return n1+n2;
}
};
var x = ops.Add(3,5); //x =8
var y = ops.AddNumbers(3,5); //
Add:function AddNumbers(n1,n2)
{
return n1+n2;
}
};
var x = ops.Add(3,5); //x =8
var y = ops.AddNumbers(3,5); //
not valid
Encapsulation-in-JavaScript
Example
var x = 2000;
var fun = function Encas()
{
var y = 12;
return y;
};
var total = x+y; //
var total = x+Encas()//total is 2012
var fun = function Encas()
{
var y = 12;
return y;
};
var total = x+y; //
Invalid use of y
var total = x+Encas()//total is 2012
Functions in Functions
Example
function OuterFunction(n)
{
function InnerFunction()
{
return n*n;
}
return InnerFunction();
}
var x = OuterFunction(10);// x = 100
//InnerFunction can not call direcly
{
function InnerFunction()
{
return n*n;
}
return InnerFunction();
}
var x = OuterFunction(10);// x = 100
//InnerFunction can not call direcly
Immediate Functions
Example
(
function() {...}()
)
;or
(
function() {...}
)
();Module Pattern
Extended immediate functionExample
var fun = (function(){
var m=2000,c=0,d=10,y=2;
return {
GetHiddenYear:function()
{
return m+c+d+y;
}
}
}());
var x = fun.GetHiddenYear(); //x==2012;
var m=2000,c=0,d=10,y=2;
return {
GetHiddenYear:function()
{
return m+c+d+y;
}
}
}());
var x = fun.GetHiddenYear(); //x==2012;
Passing function as a parameter
Example
function Add(n1,n2)
{
return n1+n2;
}
function calc(n1,n2,processForCalc)
{
return processForCalc(n1,n2);
}
function executeMath()
{
log(calc(4,4,Add));//output 8
}
{
return n1+n2;
}
function calc(n1,n2,processForCalc)
{
return processForCalc(n1,n2);
}
function executeMath()
{
log(calc(4,4,Add));//output 8
}
Published 12:22 AM by Aruna Jayathilaka with 0 comment
HTML5 Web Storage
HTML5 web storage, a better local storage than cookies.
What is HTML5 Web Storage?
With HTML5, web pages can store data locally within the user's browser.Earlier, this was done with cookies. However, Web Storage is more secure and faster. The data is not included with every server request, but used ONLY when asked for. It is also possible to store large amounts of data, without affecting the website's performance.
The data is stored in key/value pairs, and a web page can only access data stored by itself.
Browser Support
Web storage is supported in Internet Explorer 8+, Firefox, Opera, Chrome, and Safari.
Note: Internet Explorer 7 and earlier versions, do not support web storage.
localStorage and sessionStorage
There are two new objects for storing data on the client:- localStorage - stores data with no expiration date
- sessionStorage - stores data for one session
if(typeof(Storage)!=="undefined")
{
// Yes! localStorage and sessionStorage support!
// Some code.....
}
else
{
// Sorry! No web storage support..
}
{
// Yes! localStorage and sessionStorage support!
// Some code.....
}
else
{
// Sorry! No web storage support..
}
The local Storage Object
The local Storage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.Example
localStorage.lastname="Smith";
document.getElementById("result").innerHTML="Last name: "
+ localStorage.lastname;
document.getElementById("result").innerHTML="Last name: "
+ localStorage.lastname;
Try it yourself »
- Create a localStorage key/value pair with key="lastname" and value="Smith"
- Retrieve the value of the "lastname" key and insert it into the element with id="result"
The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:
Example
if (localStorage.clickcount)
{
localStorage.clickcount=Number(localStorage.clickcount)+1;
}
else
{
localStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s).";
{
localStorage.clickcount=Number(localStorage.clickcount)+1;
}
else
{
localStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s).";
Try it yourself »
The sessionStorage Object
The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the browser window.The following example counts the number of times a user has clicked a button, in the current session:
Example
if (sessionStorage.clickcount)
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + sessionStorage.clickcount + " time(s) in this session.";
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + sessionStorage.clickcount + " time(s) in this session.";
Try it yourself » Refferenced from W3school »
Your suggestion:
Close [X]
Thank you for your support.
Close [X]
Wednesday, December 26, 2012
Published 10:14 PM by Google Map with 0 comment
What is the Web workers
A server-sent event is when a web page automatically gets updates from a server.
This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.
Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.
ΙΕ not Support.
A server-sent event is when a web page automatically gets updates from a server.
This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.
Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.
ΙΕ not Support.
var source=new EventSource("sample.php"); source.onmessage=function(event) { document.getElementById("result").innerHTML+=event.data + "
"; };
Published 9:38 PM by Google Map with 0 comment
What is the Web workers
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.
ΙΕ not Support.
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<script>
var w;
function startWorker()
{
if(typeof(Worker)!=="undefined")
{
if(typeof(w)=="undefined")
{
w=new Worker("demo_workers.js");
}
w.onmessage = function (event) {
document.getElementById("result").innerHTML=event.data;
};
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
}
}
function stopWorker()
{
w.terminate();
}
</script>
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.
ΙΕ not Support.
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<script>
var w;
function startWorker()
{
if(typeof(Worker)!=="undefined")
{
if(typeof(w)=="undefined")
{
w=new Worker("demo_workers.js");
}
w.onmessage = function (event) {
document.getElementById("result").innerHTML=event.data;
};
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
}
}
function stopWorker()
{
w.terminate();
}
</script>
Thursday, December 20, 2012
Published 8:28 PM by Google Map with 0 comment
This message box is designed for asp.net pages using JQuery
Monday, December 17, 2012
Published 4:36 AM by Google Map with 1 comment
DOWNLOAD SOURCE
.pic {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
border-color: #EEEEEE;
border-image: none;
border-style: solid;
border-width: 5px 5px 18px;
box-shadow: 2px 2px 3px #333333;
position: absolute;
background-color: #EEEEEE;
}
#imagecontainer{
width:100%;
height:400px;
}
</style>
<script>
function randomFromInterval(from,to)
{
return Math.floor(Math.random()*(to-from+1)+from);
}
function jsonFlickrApi(rsp) {
var s = "";
// http://farm{id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
// http://www.flickr.com/photos/{user-id}/{photo-id}
//s = "total number is: " + rsp.photos.photo.length + "<br/>";
for (var i=0; i < rsp.photos.photo.length; i++) {
//var rand = randomFromInterval(1,10);
var stage_width=800;
var stage_height=400;
var left = randomFromInterval(0,stage_width);
var top = randomFromInterval(0,400);
var rot = randomFromInterval(-40,40);
if(top>(stage_height-130) && left > (stage_width-230))
{
top-=120+130;
left-=230;
}
photo = rsp.photos.photo[i];
var _url = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_" + "m.jpg";
p_url = "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id;
s += '<div class="pic" style="top:'+top+'px;left:'+left+'px; -moz-transform:rotate('+rot+'deg); -webkit-transform:rotate('+rot+'deg);">'+
'<img src="' + _url + '"/></div>';
}
$('#imagecontainer').append(s);
}
$(document).ready(function() {
});
</script>
<div id="imagecontainer">
</div>
<script src="http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=f3c5222d18401ab6991a5532da0208b1&tags=nature&per_page=50&format=json"></script>
Subscribe to:
Posts (Atom)