Tag : javascript

Javascript simple clock

Create a simple function that get the client time (hours:minutes:seconds) and prints it in the div:


<script type="text/javascript">
function getTime() {
var currentDate = new Date();
var hour = currentDate.getHours();
var min = currentDate.getMinutes();
var sec = currentDate.getSeconds();
var currentTime = hour + ":" + min + ":" + sec;
document.getElementById('clock').innerHTML = currentTime;
}
setInterval("getTime()", 1000);
</script>
<div id="clock"></div>

Add a custom right-click function to a web application

Add a custom right-click function to a web application. Also can be used to implemen a custom menu opened on right-click:


<script language="javascript" type="text/javascript">
document.oncontextmenu = RightMouseDown;
document.onmousedown = mouseDown;
function mouseDown(e) {
if (e.which == 3) {//righClick
console.log("Right-click goes here");
}
}
function RightMouseDown() {
console.log("Right-click Context goes here");
return false;
}
</script>

Set and get cookie with Javascript

Javascript function to set cookie:


function setCookie(c_name, value, exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

Javascript function to get cookie:


function getCookie(c_name)
{
var i,x,y,ARRcookies = document.cookie.split(";");
for (i=0; i < ARRcookies.length; i++)
{
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g,"");
if (x == c_name)
{
return unescape(y);
}
}
}

Full article at: W3SCHOOLS

Loading JavaScript Asynchronously

Sample of loading javascript asyncronously:


<script id="myscript" type="text/javascript">
(function() {
var myscript = document.createElement('script');
myscript.type = 'text/javascript';
myscript.src = ('http://example.org/myscript.js');
var s = document.getElementById('myscript');
s.parentNode.insertBefore(myscript, s);
})();
</script>

Visit this page for entire article.

Javascript print array elements and print object

Print array elements with javascript:


var output = '';
for(var i=0; i < arr.length; i++){ output += "arr[" + i + "] = " + arr[i] + ";"; } alert(output);

Print object with javascript:


var output = '';
for (property in object) {
output += property + ': ' + object[property] +'; ';
}
alert(output);

Javascript – limit the number of characters in a textarea

Javascript – limit the number of characters in a textarea:


<script language="javascript" type="text/javascript">
function limitText(fieldLimit, countLimit, limitNum) {
if (fieldLimit.value.length > limitNum) {
fieldLimit.value = fieldLimit.value.substring(0, limitNum);
} else {
countLimit.value = limitNum - fieldLimit.value.length;
}
}
</script>
<form name="form_name">
<textarea name="sometext" onKeyDown="limitText(this.form.sometext,this.form.counter,100);"
onKeyUp="limitText(this.form.sometext,this.form.counter,100);">
</textarea><br />
(Maximum characters: 100)<br />
You have <input readonly type="text" name="counter" size="3" value="100"> characters left.
</form>

Read original article

Javascript – addslashes and stripslashes

A Javascript function for add slashes:


function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}

A Javascript function for strip slashes:


function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

JavaScript redirect a page after 5 seconds

Javascript function to redirect to another page after 5 seconds:


function redirect_after_5_seconds()
{
setTimeout("location.href='index.php/new_url_for_redirection'", 5000);
}

Javascript – compare two dates

Compare two dates in Javascript:


<html>
<head>
<title>JavaScript - compare two dates</title>
</head>
<body>
<script language="Javascript" type="text/javascript">
var date1 = new Date("22/12/2010");
var date2 = new Date("25/12/2010");
if (date1 <= date2)
{
alert("Date 1 lower or equal than date 2!");
}
else
{
alert("Date 2 lower than date 1!");
}
</script>
</body>
</html>

Javascript get elements by name

Javascript get elemens by name:


<html>
<head>
<script type="text/javascript">
function getInputValues(varName)
{
varValues = document.getElementsByName(varName);
for (var i = 0; i < varValues.length; i++)
{
alert(varValues[i].value);
}
}
</script>
</head>
<body>
<input type="text" name="xName" ><br />
<input type="text" name="xName" ><br />
<input type="text" name="xName" ><br />
<input type="button" onclick="getInputValues('xName'); return false;" value="Display Values">
</body>
</html>