Diferencia entre revisiones de «Usuario:Juanda/javascript/Eventos»
De WikiEducator
(Página creada con '{{Usuario:juanda/javascript/nav }} {{#widget:Slides}} {{MiTitulo| Curso de JavaScript}} <div class="slides layout-regular template-default"> <div class="slide"> == Eventos ==…') |
|||
| Línea 60: | Línea 60: | ||
</html> | </html> | ||
</source> | </source> | ||
| + | </div> | ||
| + | <div class="slide"> | ||
| + | ===Captura de errores=== | ||
| + | *El '''evento onerror''' se dispara cuando hay un error de script en la página | ||
| + | *Se debe crear una función que maneje el error. | ||
| + | *La función se llama desde el manejador de eventos de onerror con 3 argumentos: | ||
| + | :msg (mensaje de error) | ||
| + | :url (la url de la página que ha causado el error) | ||
| + | :line (la línea en la que ha ocurrido el error). | ||
| + | *Ejemplo captura de error con onerror: | ||
| + | <source lang="html4strict"> | ||
| + | <html> | ||
| + | <head> | ||
| + | <script type="text/javascript"> | ||
| + | onerror=handleErr; | ||
| + | var txt=""; | ||
| + | function handleErr(msg,url,l) | ||
| + | { | ||
| + | txt="Ha habido un error en esta página.\n\n"; | ||
| + | txt+="Error: " + msg + "\n"; | ||
| + | txt+="URL: " + url + "\n"; | ||
| + | txt+="Línea: " + l + "\n\n"; | ||
| + | txt+="Pulsa ok para continuar.\n\n"; | ||
| + | alert(txt); | ||
| + | return true; | ||
| + | } | ||
| + | function mensaje() | ||
| + | { | ||
| + | adddlert("¡Bienvenido!"); | ||
| + | } | ||
| + | </script> | ||
| + | </head> | ||
| + | <body> | ||
| + | <input type="button" value="View message" onclick="mensaje()" /> | ||
| + | </body> | ||
| + | </html> | ||
| + | </source> | ||
| + | </div> | ||
| + | |||
| + | <div class="slide"> | ||
| + | ===try....catch y throw=== | ||
| + | Se pueden lanzar errores mediante throw: | ||
| + | <source lang="html4strict"> | ||
| + | <html> | ||
| + | <body> | ||
| + | <script type="text/javascript"> | ||
| + | var x=prompt("Introduce un número entre 0 y 10:",""); | ||
| + | try | ||
| + | { | ||
| + | if(x>10) | ||
| + | throw "Error1"; | ||
| + | else if(x<0) | ||
| + | throw "Error2"; | ||
| + | } | ||
| + | catch(er) | ||
| + | { | ||
| + | if(er=="Error1") | ||
| + | alert("¡Error! El valor es muy alto"); | ||
| + | if(er == "Error2") | ||
| + | alert("¡Error! El valor es muy bajo"); | ||
| + | } | ||
| + | </script> | ||
| + | </body> | ||
| + | </html> | ||
| + | |||
| + | </source> | ||
</div> | </div> | ||
</div> | </div> | ||
Revisión de 22:40 27 sep 2012