So far we have seen how to trap the javascript errors in Part 1. Now we have
the data. We will see now, how to send data to the server using AJAX.
Send Data to Server
ajaxCtrl(
function(){
return true;
},"ajxerrorLogger.php",theData
);
The remote file is “ajxerrorLogger.php”.
I’ve used PHP for this.
Instead you can use ASP, JSP also.
This is used to receive the
data sent from the JavaScript error handling function. That’s it.
Our JavaScript errors are now sent to the server silently.
What Next? In the server side, we have to code the “ajxerrorLogger.php”
Receive Data and Write into a File
if($_POST && $_POST['file']!=”){
$filename = “./errlogs.txt”;
$fh = fopen($filename,”a+”);
//the content is in the form
//Date File LineNo Error Message
//(tab delimited)
$fcontent = date(”d/m/Y h:i:s”, mktime()).”\t”.
$_POST['file'].”\t”.$_POST['line'].”\t”.$_POST['err'].”\r\n”;
if (is_writable($filename)) {
if (fwrite($fh, $fcontent) === FALSE) {
}
fclose($fh);
}
}
$fh = fopen($filename,”a+”);
Opens the File errlogs.txt in append mode. So the new errors
will be added into the file without overwriting it.
We are going to save the data ( js errors ) as tab
delimited. For the sake reference we can store the date and time also.
$fcontent=date("d/m/Yh:i:s", mktime()). "\t".
$_POST['file']. “\t” .$_POST['line'].
“\t”.$_POST['err'].”\r\n”;
Now the data is ready to be written into the file.
fwrite($fh, $fcontent)
writes the data into the file. For
extra error handling we can check,
if the file is writable.
if (is_writable($filename)) {
Will check and return TRUE if the file is writable. Then we
can write the data. That’s it. We
have successfully logged our JavaScript
errors in the server silently and we can see the errors
that are occurring in
the client side, while they are using the site.
See the Errors real-time?
How? We have to add a small, separate PHP file to view the logs
we have generated above. The script will read the file and show
the contents.
if (file_exists($filename) && is_readable($filename)) { //if exists and readable
$contents = fread($fh,filesize ($filename)); //read the whole file
fclose($fh); //and close
}
$splitcontents = explode($linedelim, $contents);
...
Add some lines to show each line read from the file. We are done.
Summary
- We have written a JavaScript error trapping code, which will suppress the errors
- Added Ajax Code To send the trapped errors to the server
- Receive the data and store into a flat file using PHP
- Added a small script to see the logged JavaScript errors remotely.
Improvements
- The script can be further improved by adding capability to send the logs in mail to the administrator.
- Instead of writing into a flat file, we can store into a database.
- The same thing can be done using PHP as well as ASP, JSP also.
Alternative Way
Every coin have two sides. So do we. We can do the same thing without using AJAX. How?
An easy of doing this is to use an image. Yes. “img” tag will do.
var img = new Image();
img.src = “/ajxerrorLogger.php?” + theData;
A Note of caution and convenience, This can be used across the domains also.
Download
The entire source code and a ready to use sample is here.
That’s it. Have fun.
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on March 12th, 2008 with no comments.
Read more articles on Ajax and Browsers and Javascript and PHP.
Introduction
Recently in one of my projects, I’m informed by my QA team
that, in some pages they’re experiencing crashes, or some pages not functioning
properly after sometime.
Our team looked for options where there can be a code
generating errors. And the feedback I got, from the QA team was not helpful to
reproduce the error. But there is no doubt to look for areas only in JavaScript
coding, and not other than JavaScript coding.
Normally while releasing a project, what I would do is, just
escape/bypass the JavaScript errors. That means, whenever there is a JavaScript
error, it will not be reported to the user and they are suppressed.
Background
Following is the function, we can normally use to suppress the JavaScript errors.
var isDebugging = true;
function ErrorSetting(msg, file_loc, line_no) {
var e_msg=msg;
var e_file=file_loc;
var e_line=line_no;
var error_d = "Error in file: " + file_loc +
"\nline number:" + line_no +
"\nMessage:" + msg;
if(isDebugging)
alert("Error Found !!!\n--------------\n"+error_d);
return true;
}
window.onerror = ErrorSetting;
window.onerror = ErrorSetting; this line will call the ‘ErrorSetting’
function, whenever there is a JavaScript error. And the function will return
true. So the user will not get JavaScript
error notifications and won’t be
annoyed.
While in the development stage the variable ‘isDebugging’ is
set as true, and we can get
the JavaScript alerts, notifying the error with the
line no, the filename and the error message.
The above method is commonly used by many web developers.
This is the same I were using in my previous projects.
But in this particular project, I have to get error messages
in the real-time when they are using, immediately in my mind I got an idea of
logging the errors. (Later I searched thru the web and there are many
developers have done it like this!? 
)
But logging into a file in the client side is having two
issues.
1. Not supported in browsers other than IE (Only using
ActiveX Objects)
2. The files will be stored in the client side not in
server.
So I used AJAX,
to silently send the JavaScript errors to the server, where the errors
Will be, written in a flat file. And later we can view the
file for error logs.
What next ?
Started adding AJAX
code into the function.
Trap Errors and Prepare the Data
var isDebugging = false;
var logJsErrors = true;
function ErrorSetting(msg, file_loc, line_no) {
var e_msg=msg;
var e_file=file_loc;
var e_line=line_no;
var error_d = "Error in file: " + file_loc +"\nline number:"
+ line_no +
"\nMessage:" + msg;
if(logJsErrors){
theData = "file="+file_loc+"&line="+line_no+"&err="+msg;
ajaxCtrl(
function(){
return true;
},"ajxerrorLogger.php",theData
);
}
if(isDebugging)
alert("Error Found !!!\n--------------\n"+error_d);
return true;
}
window.onerror = ErrorSetting;
What the above function does?
if(logJsErrors){ //this line is used for enable or disable
Logging.
The line no, file name and the error message are combined as
key value pair.theData =
“file=”+file_loc+”&line=”+line_no+”&err=”+msg;
Now the data is ready, we can now call the AJAX function that will send the data to the
server.
Continued. in Part 2
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on February 29th, 2008 with 3 comments.
Read more articles on .NET and Ajax and Javascript and PHP.
Released my first Firfox Plugin
This plugin will get Updates on your desired Stocks from Bombay and National exchanges .
Read more here
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on November 27th, 2007 with no comments.
Read more articles on Browsers and Javascript and Plugins.
The IE Search plugin for Experts Exchange.com is here
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on July 17th, 2007 with 1 comment.
Read more articles on Browsers and IE7 and Plugins.
The Firefox Search plugin for Experts Exchange.com is here
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on July 14th, 2007 with no comments.
Read more articles on Browsers and Firefox and Plugins.
To create a shortcut to an url you can do as follows
set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.TargetPath = "http://www.yahoo.com"
oUrlLink.Save
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on June 25th, 2007 with 1 comment.
Read more articles on WSH/Wscript.
If you want to create a shortcut programatically use the code below. Found somewhere on net
'Create a Desktop ShortCut
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut To Notepad.lnk")
oShellLink.TargetPath = "Notepad.exe" 'WScript.ScriptFullName
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+N"
oShellLink.IconLocation = "notepad.exe, 0"
oShellLink.Description = "Shortcut To Notepad."
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on June 25th, 2007 with no comments.
Read more articles on WSH/Wscript.
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "Computer Name = " & WshNetwork.ComputerName
WScript.Echo "User Name = " & WshNetwork.UserName
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on June 25th, 2007 with no comments.
Read more articles on WSH/Wscript.
Option Explicit
Dim gpsFile
Dim gpsFileLines
Dim eGpsLine
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oWS : Set oWS = CreateObject("WScript.Shell")
gpsFile = "c:\yourtextfile.txt"
With oFSO.GetFile(gpsFile)
'Get all Lines
gpsFileLines = Split(.OpenAsTextStream(1, 0).Read(.Size), vbcrlf)
End With
For Each eGpsLine in gpsFileLines
'Print all lines on by one
Wscript.Echo eGpsLine & vbcrlf
Next
Wscript.Quit
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on June 25th, 2007 with no comments.
Read more articles on WSH/Wscript.
We can get the Processor and OS info like this using the Environment method
Set WshShell = WScript.CreateObject(”WScript.Shell”)
Set WshSysEnv = WshShell.Environment(”SYSTEM”)
WScript.Echo “NUMBER OF PROCESSORS : ” & WshSysEnv(”NUMBER_OF_PROCESSORS”)
WScript.Echo “PROCESSOR ARCHITECTURE : ” & WshSysEnv(”PROCESSOR_ARCHITECTURE”)
WScript.Echo “PROCESSOR IDENTIFIER : ” & WshSysEnv(”PROCESSOR_IDENTIFIER”)
WScript.Echo “OS : ” & WshSysEnv(”OS”)
BookMark this Post:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Written by Kumar S on June 25th, 2007 with no comments.
Read more articles on WSH/Wscript.
« Older articles
No newer articles