|
Recupération des erreurs Javascript |
|
|
|
|
Écrit par DOTNET
|
|
09-08-2004 |
Si on considère le fichier HTML suivant : <html> <head> <title>Gestion d'erreur Javascript</title> <script type="text/javascript" src="error.js"></script> </head> <body> <script type="text/javascript"> try{ //génération d'une erreur eval( "6 + * 3" ) }catch(oException){ postError( 'index.html', oException.message ); } </script> </body> </html>
Puis le fichier "error.js" suivant : function postError( sPosition, sMessage ){ try{ var sPageRequest = "error.php"; var xhr_object = null; if( window.XMLHttpRequest ) // -> Firefox xhr_object = new XMLHttpRequest(); else if( window.ActiveXObject ) // -> Internet Explorer xhr_object = new ActiveXObject( "Microsoft.XMLHTTP" ); //requête synchrone xhr_object.open( "POST", sPageRequest, false ); xhr_object.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" ); xhr_object.send( "pos=" + encodeURI( sPosition ) + "&mes=" + encodeURI( sMessage ) ); }catch( oException ){ return oException; } } On peut récupérer facilement les erreurs dans un fichier "error.php" : <?php //récupération de l'erreur $oError = (object)NULL; $oError->sPosition = $_REQUEST[ 'pos' ]; $oError->sMessage = $_REQUEST[ 'mes' ]; ?> |
|
Dernière mise à jour : ( 12-06-2008 )
|