An easy way to log client side information to server
What should we do?
The first thought comes into our mind may be using AJAX, since it involves client server communication and we also should make it asynchronously because we don't want to interrupt users while we are logging something to server. This is feasible, but it needs many codes to be written to create the XMLHttpRequest object and implements some function related to it, it is also error prone since the communication may be interrupted as well.
1. We need to create a backend server script to get the passed query strings and store them on the server. We assume it is logger.php here;
var img=new Image();
3. Call the log() function like :
That's all. You can simply log information to server now.
In the log() function, we created an Image object, it is supported in all modern browsers, later when we assign the src property, the image will be loaded immediately. this will load the logger.php script with the passed query string. It has similar effect as the AJAX method.
- The Image object is available in all browsers, even those that don’t support the XMLHttpRequest object.
- Cross-domain restrictions don’t apply. Often there is one server responsible for handling error logging from multiple servers, and XMLHttpRequest would not work in that situation.