Mostrando entradas con la etiqueta Javascript. Mostrar todas las entradas
Mostrando entradas con la etiqueta Javascript. Mostrar todas las entradas

lunes, 22 de septiembre de 2008

Recorrer todos los controles de una Web

Este código permite indicando una etiquta Html recorrer todos los controles que se
encuentren dentro de este.
En este caso se recorren todos los controles que son de tipo texto y los deshabilita


function DeshabilitarCajasTexto('etiquetaEnglobaControles')
{
for (i=0; i < document.getElementById(control).all.length; i++)
{
if (document.getElementById(control).all[i].type == 'text')
document.getElementById(control).all[i].disabled =true;
}
}

viernes, 19 de septiembre de 2008

Obtener parametros URL

Para obtener todos los parámetros de una queryString mediante JavaScript podemos usar una de estas dos funciones:

Primero guardamos la Url que tenemos en el navegador

var miUrl=window.location.href;

Con esta primera función se le pasará la url y el nombre del parámetro del que se quiere obtener el valor:

var anio = obtenerParametro(miUrl,'Anio');
function obtenerParametro(miUrl,nomParametro)
{
var parametro = "";
var empiezaParametro = miUrl.indexOf(nomParametro);
empiezaParametro = empiezaParametro + nomParametro.length + 1;
if(empiezaParametro != -1);
{
var urlAux = miUrl.substring(empiezaParametro,miUrl.length);
var terminaParametro = urlAux.indexOf('&');
if(terminaParametro != -1)
parametro = miUrl.substring(empiezaParametro,empiezaParametro + terminaParametro);
else
parametro = miUrl.substring(empiezaParametro,miUrl.length);

}
return parametro;
}

En esta función obtiene todos los parámetros que se estan pasando.

var miUrl=obtenerTodosParametros(window.location.href);

function obtenerTodosParametros(miUrl)
{
var parametro = "";
var empiezaParametro = miUrl.indexOf('?');
if(empiezaParametro == '0');
{
var terminaParametro = miUrl.length;
parametro = miUrl.substring(empiezaParametro,terminaParametro);
}
return parametro;
}

miércoles, 17 de septiembre de 2008

Convertir string a int o float

Question: How do I convert strings to numbers in JavaScript?

Answer: To convert a string to a number, use the JavaScript function parseFloat (for conversion to a floating-point number) or parseInt (for conversion to an integer).

parseFloat syntax: parseFloat('string')

How it works:
The argument of parseFloat must be a string or a string expression. The result of parseFloat is the number whose decimal representation was contained in that string (or the number found in the beginning of the string). If the string argument cannot be parsed as a decimal number, the results will be different in different browsers (either 0 or NaN).

Examples (comments in each line give the conversion results):

parseFloat('1.45kg')  // 1.45
parseFloat('77.3') // 77.3
parseFloat('077.3') // 77.3
parseFloat('0x77.3') // 0
parseFloat('.3') // 0.3
parseFloat('0.1e6') // 100000

parseInt syntax: parseInt( 'string' [, base] )

How it works:
The first argument of parseInt must be a string or a string expression. The result returned by parseInt is an integer whose representation was contained in that string (or the integer found in the beginning of the string). The second argument (base), if present, specifies the base (radix) of the number whose string representation is contained in the string. The base argument can be any integer from 2 to 36.

If there is only one argument, the number base is detected according to the general JavaScript syntax for numbers. Strings that begin with 0x or -0x are parsed as hexadecimals; strings that begin with 0 or -0 are parsed as octal numbers. All other strings are parsed as decimal numbers.

If the string argument cannot be parsed as an integer, the results will be different in different browsers (either 0 or NaN).

Examples (comments in each line give the conversion results):

parseInt('123.45')  // 123
parseInt('77') // 77
parseInt('077',10) // 77
parseInt('77',8) // 63 (= 7 + 7*8)
parseInt('077') // 63 (= 7 + 7*8)
parseInt('77',16) // 119 (= 7 + 7*16)
parseInt('0x77') // 119 (= 7 + 7*16)
parseInt('099') // 0 (9 is not an octal digit)
parseInt('99',8) // 0 or NaN, depending on the platform
parseInt('0.1e6') // 0
parseInt('ZZ',36) // 1295 (= 35 + 35*36)

lunes, 15 de septiembre de 2008

Imprimir una determinada parte de la página

En el código HTML incluiremos una etiqueta DIV por ejemplo con ID="Imprimir", introduciendo en ella la parte de la página que se quiere imprimir.

Aquí obtenemos todo el código que está dentro del DIV

var htmlImpresion= document.getElementById('Imprimir');

Creamos una ventana de presentación

var ventimp = window.open(' ', 'popimpr');

En ella escribimos lo que queramos que se imprima

ventimp.document.write(htmlImpresion);
ventimp.document.close();
ventimp.print();
ventimp.close();