Tag Archive for javascript

Flip selected checkboxes

function invertSelection()
{
var checkboxes = document.theform.boxesname;
for(var i=0; i<checkboxes.length; i++)
{
checkboxes[i].checked = !(checkboxes[i].checked);
}
}

source

Get Javascript Random Value

public static string GetJavascriptRandomValue()
{
Random ranDouble = new Random();
string strd1 = ranDouble.NextDouble().ToString();
int strd1len = strd1.Length;
Random ranLong = new Random();
string strd2 = ranLong.Next().ToString() + ranLong.Next().ToString();
int intDefLength = 18;
string strCutFromstrd2 = string.Empty;
if (strd1len < intDefLength)
{
strCutFromstrd2 = strd2.Substring(0, intDefLength - strd1len);
}
string strRes = strd1 + strCutFromstrd2;
return strRes;
}

source

Get Java Script Time

public static string GetJavaScriptTime()
{
Int64 retval = 0;
DateTime st = new DateTime(1970, 1, 1);
TimeSpan t = (DateTime.Now - st);
retval = (Int64)(t.TotalMilliseconds + 0.5);
retval = retval + 1000000;
return retval.ToString();
}

source

DIV instead SELECT tag ()

<html>
<head>
<script type='text/javascript' src='js/prototype.js'></script>
<script type='text/javascript'>
function change_country(type, value) {
$('country_type').value=type;
$('country_value').value=value;
}
</script>
<style type="text/css">
#select_countries {
border: 1px solid #333;
margin-top: 5px;
height: 200px;
width: 200px;
overflow: auto;
}
</style>
</head>
<body>
<form action="" method="post">
<fieldset>
<legend>Countries</legend>
<input type="text" name="country_value" id="country_value" value="United States" />
<input type="hidden" name="country_type" id="country_type" value="us" />
<a href="#" onclick="Element.show('select_countries'); return false;">Click to change</a><br />
<div id="select_countries" style="display: none;">
<div id="close" onclick="Element.hide('select_countries'); return false;">Close</div>
<div onclick="change_country('us', 'United states')"><img src="images/flags/us.gif" alt="us" /> United states</div>
<div onclick="change_country('ru', 'Russia')"><img src="images/flags/ru.gif" alt="us" /> Russia</div>
<div onclick="change_country('es', 'Spain')"><img src="images/flags/es.gif" alt="us" /> Spain</div>
<div onclick="change_country('ca', 'Canada')"><img src="images/flags/ca.gif" alt="ca" /> Canada</div>
</div>
</fieldset>
</form>
</body>
</html>

source

Handling element focus with tab

Event.observe($('body'), 'keypress', function(event){
if(event.keyCode == Event.KEY_TAB) {
$('sendMessage').focus();
Event.stop(event);
}});

source

Java i18n to JavaScript Object action

<%@ page import="java.util.*, my.utils" %>
<%response.setContentType("text/javascript");%>
if (!window.i18n){window.i18n = {};}
<%
Locale currentLocale = UtilJSP.getCurrentLocale(request);
ResourceBundle labels = ResourceBundle.getBundle(my.utils.getI18nResources(), currentLocale);
Enumeration bundleKeys = labels.getKeys();

while (bundleKeys.hasMoreElements()){
String key = (String)bundleKeys.nextElement();
String value = labels.getString(key);

if (request.getParameter("filter") != null && key.startsWith(request.getParameter("filter"))){
out.println("window.i18n["" + key + ""] = "" + value.replaceAll(""", "\"").replaceAll("n", "\n") + "";");
}
}
%>

source

JavaScript i18n String object & localize method

String.prototype.localize = function(){
var cReturnValue = (i18n) ? i18n[this] : this;

if(!cReturnValue){
cReturnValue = "§§§" + this + "§§§";
} else {
for (var i = 0; i < arguments.length; i++){
cReturnValue = cReturnValue.replace("{" + i + "}", arguments[i]);
}
}
return cReturnValue;
};

source

Javascript Closure

/*
* Since we are passing the inner function outside, a reference to it
* still exists, hence it is not collected by the garbage collector.
*
* The variable a persists through to the next increment, and is not overwritten
* by outer() var a = 0 since inner still has a local variable inside it's scope.
*
* By creating global2, you have created another scope that acts independently,
* even though they are using the same function literal!
*/
function outer(){//define outer scope
var a = 0;//this is the persistent variable
function inner(){//define the inner scope
a++;//increment a
console.log(a);
}
return inner;//IMPORTANT: return the inner function to the outside (scope)
}
var global = outer();//create a new scope in global
global();//a = 1
global();//a = 2
var global2 = outer();
global2();//a = 1
global2();//a = 2

source

Round to number of decimals

Math.round(value*10000)/10000

source

prototype class creation

//Use with prototype 1.6
var Person = Class.create({

initialize: function(){
//Create a Hash for information store
this.info = $H();
}
,
setName: function(name){
this.info.set("name",name);
}
,
getName: function(){
return this.info.get("name");
}
,
setSurname: function(surname){
this.info.set("surname",surname);
}
,
getSurname: function(){
return this.info.get("surname");
}
,
toJSON: function(){
return this.info.toJSON();
}
});

source