Skip to main content

Posts

Print a page in javascript

Many times you would like to give a button at your webpage to print out the content of that web page via an actual printer. JavaScript helps you to implement this functionality using print function of window object. The JavaScript print function window.print() will print the current web page when executed. You can call this function directly using onclick event as follows: <head> <script type="text/javascript"> <!-- //--> </script> </head> <body> <form> <input type="button" value="Print" onclick="window.print()" /> </form> </body> This will produce following button which let you print this page. This serves your purpose to get page printed out, but this is not a recommended way of giving printing facility. A printer friendly page is really just a page with text, no images, graphics, or advertising. You can do one of the followings to make a page printer friendly: Mak

Form Validation in JavaScript

Form validation used to occur at the server, after the client had entered all necessary data and then pressed the Submit button. If some of the data that had been entered by the client had been in the wrong form or was simply missing, the server would have to send all the data back to the client and request that the form be resubmitted with correct information. This was really a lengthy process and over burdening server. JavaScript, provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions. Basic Validation - First of all, the form must be checked to make sure data was entered into each form field that required it. This would need just loop through each field in the form and check for data. Data Format Validation - Secondly, the data that is entered must be checked for correct form and value. This would need to put more logic to test correctness of data. We will ta

Error Handling in JavaScript

There are three types of errors in programming: (a) Syntax Errors and (b) Runtime Errors (c) Logical Errors: Syntax errors: Syntax errors, also called parsing errors, occur at compile time for traditional programming languages and at interpret time for JavaScript. For example, the following line causes a syntax error because it is missing a closing parenthesis: <script type="text/javascript"> <!-- window.print(; //--> </script> When a syntax error occurs in JavaScript, only the code contained within the same thread as the syntax error is affected and code in other threads gets executed assuming nothing in them depends on the code containing the error. Runtime errors: Runtime errors, also called exceptions, occur during execution (after compilation/interpretation). For example, the following line causes a run time error because here syntax is correct but at run time it is trying to call a non existed method: <script type="text/

Difference Between GET and POST methods

GET: 1) Data is appended to the URL. 2) It is a single call system. 3) Maximum data that can be sent is 256. 4) Data transmission is faster. 5) This is the default method for many browsers.  6) GET method have not security because of data view in address bar. For ex: we can not use this method for checking log in. 7) We can bookmark link with this method. 8) Server can log all action. 9) because of server log, we can pass 255 char. as query  string. 10) It can store only 18 form variable. POST: 1) Data is not appended to the URL. 2) It is a two call system. 3) There is no Limit on the amount of data.That is characters any amount of data can be sent. 4) Data transmission is comparatively slow. 5) No default and should be Explicitly specified. 6) We can not bookmark page link. 7) We can pass unlimited data by this method as query string. 8) It is more secure but slower as compared to GET. 9) POST form contents are passed to the script as an input file.

How To Optimize Your Site With GZIP Compression

Compression is a simple, effective way to save bandwidth and speed up your site. I hesitated when recommending gzip compression when speeding up your javascript because of problems in older browsers . But it's the 21st century. Most of my traffic comes from modern browsers, and quite frankly, most of my users are fairly tech-savvy. I don't want to slow everyone else down because somebody is chugging along on IE 4.0 on Windows 95. Google and Yahoo use gzip compression. A modern browser is needed to enjoy modern web content and modern web speed -- so gzip encoding it is. Here's how to set it up. Wait, wait, wait: Why are we doing this? Before we start I should explain what content encoding is. When you request a file like http://www.yahoo.com/index.html , your browser talks to a web server. The conversation goes a little like this: 1. Browser: Hey, GET me /index.html 2. Server: Ok, let me see if index.html is lying around... 3. Server: Found it!

Life cycle of Servlet

The life cycle of a servlet can be categorized into four parts: Loading and Inatantiation:  The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet. Initialization:  After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist untill the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet. The servlet will be available for service if it

Methods of Servlets

A Generic servlet contains the following five methods: init() public void init(ServletConfig config) throws ServletException The  init() method  is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed into service.  The servlet cannot be put into the service if  The init() method does not return within a fix time set by the web server.   It throws a ServletException Parameters - The init() method takes   a  ServletConfig  object that contains the initialization parameters and servlet's configuration and throws a  ServletException  if an exception has occurred. service() public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException Once the servlet starts getting the requests, the service() method is called by the servlet container to respond. The servlet services the client's request with the help of two objects. These two objects jav