wtorek, 5 stycznia 2010

Przydatne funkcje w JS

iFRAME
Mamy strone w niej dwie ramki , chcemy wykonać przekierowanie ramki b z ramki a, rozwiązanie:

window.parent.document.getElementById('').src = '';


Ciasteczka

function Set_Cookie( name, value, expires, path, domain, secure )
{

var today = new Date();
today.setTime( today.getTime() );


if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}




Pokazywanie / ukrywanie diva

function DynamicDiv_NormalShow(div)
{
document.getElementById(div).style.visibility = 'visible';
}


function DynamicDiv_Hide(div)
{
document.getElementById(div).style.visibility = 'hidden';
}


Zbieranie wartośći z n textboxow

tbxName - prefix textboxa
indexStart - poczatkowy index
counter - ilosc kontrolek
function getValuesfromTexBox(tbxName, indexStart, counter)
{
var result = '';
for ( i = indexStart ; i < indexStart + counter ; i++ )
{
var tbx = document.getElementById(tbxName + '_' + i)
result = result + tbx.value + '~';
}
return result;
}


Pobranie wybranych wartosci z radio button list
function getSelectedValueFromRBL(radioList, counter)
{
var result = '';
for ( i = 0 ; i < counter ; i++ )
{
var cbx = document.getElementById(radioList + '_' + i)

if ( cbx.checked == true )
{
result = result + '1~';
}
else
{
result = result + '0~';
}

}
return result;
}



Dynamiczne odkrywanie i chowanie diva
function SetHeightUp(div, h)
{
if ( document.getElementById(div).style.height.replace("px","") < h )
{
document.getElementById(div).style.height = parseInt(document.getElementById(div).style.height.replace("px","")) + 10 + 'px';
var meth = "SetHeightUp('" + div +"'," + h +")";
t=setTimeout(meth,10)
}
//document.getElementById(div).style.display = 'block';
}

function SetHeightDown(div, h)
{
if ( document.getElementById(div).style.height.replace("px","") > h )
{
document.getElementById(div).style.height = parseInt(document.getElementById(div).style.height.replace("px","")) - 10 + 'px';
var meth = "SetHeightDown('" + div +"'," + h +")";
t=setTimeout(meth,5)
}
// document.getElementById(div).style.display = 'none' ;
}







Yes / NO

function GetYesOrNo(question){
var returnval=confirm(question)
return returnval;
}




Validacja adresu email:

function Validators_CheckEmail(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

Brak komentarzy:

Prześlij komentarz