Monday, April 30, 2012

Can we Zoom in / Zoom out a web page using Java Script and HTML

Hi,

               A lot of websites on internet provide users with an option to increase the font size of the page for better reading experience. Although nowadays almost all the web browsers supports zoom in/out functionality using mouse wheel scroll or other shortcut keys, it is sometimes a good idea to give user a way to increase/decrease the font size of web page.
Let us implement a simple zoomer code in JavaScript that will increase/decrease the font size of the web page at client side without reloading the page from server. A small JavaScript snippet will be used to change the font size of the web page at client side.

Knowledge Base:

http://viralpatel.net/blogs/2009/03/implement-simple-font-zoomer-in-javascript-html.html

JavaScript code

Let us check the following JavaScript code.










var fontSize = 1;
function zoomIn() {
    fontSize += 0.1;
    document.body.style.fontSize = fontSize + "em";
}
function zoomOut() {
    fontSize -= 0.1;
    document.body.style.fontSize = fontSize + "em";
}
In the above JavaScript code, a global variable fontSize is defined with value 1. This variable’s value will be changed whenever user tries to increase or decrease the font size. Then we have two functions zoomIn() and zoomOut() to perform zoom in and zoom out respectively. The font size of the web page is changed by changing the attribute fontSize in document.body.style.

Calling JavaScript from HTML

You can place simple icons to zoom in and zoom out the text and call the zoomIn() and zoomOut() methods on click event. Or can place buttons to zoom in/out.



<input type="button" value="+" onClick="zoomIn()"/>
<input type="button" value="-" onClick="zoomOut()"/>
 
Thanks,
 
Rajesh Kumar yuvaraj

Open A New Window without Back Button

Hi Guys,

              Following are few tricks that can be used to disable the back button in browser. Please note that we do not literally “disable” the back button, but just nullify its effect is some case and hide it altogether in others.

Knowledge Base: http://viralpatel.net/blogs/2009/11/disable-back-button-browser-javascript.html


window.open ("http://viralpatel.net/blogs/","mywindow","status=1,toolbar=0");
Also it is possible to disable the right click on any webpage using Javascript. Add following code in the webpage.

1
<body oncontextmenu="return false;">
 

Disable Back functionality using history.forward

This is another technique to disable the back functionality in any webpage. We can disable the back navigation by adding following code in the webpage. Now the catch here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1. In this case all following code in page

<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">
The above code will trigger history.forward event for page1. Thus if user presses Back button on page2, he will be sent to page1. But the history.forward code on page1 pushes the user back to page2. Thus user will not be able to go back from page1.
 

Thursday, April 26, 2012

Convert Arrays to Set in Java

Here is a example which demonstrates conversion of Array to Set in Java

Example: Java Array to Set





String [] countires = {"India", "Switzerland", "Italy"};
Set<String> set = new HashSet<String>(Arrays.asList(countires));
System.out.println(set);
 Output:
[Italy, Switzerland, India]

Creating a ZIP file using JavaScript

Hi Guys,
             We have 'n' number of tools which generates  ZIP files but creating the ZIP files using JavaScript is different. To create a ZIP file using Java script we need to download the jszip.js and include it to your html file.


Knowledge Base : 




Sample Code:
            http://viralpatel.net/blogs/2012/01/create-zip-file-javascript.html

Step 1. Import jszip JavaScript

Include the jszip javascript file in the HTML document where you want to generate ZIP files. You can download the jszip package and include it in HTML or directly include the jszip javascript through git repository.

<script type="text/javascript"
        src="https://raw.github.com/Stuk/jszip/master/jszip.js"></script>

Step 2. Generate Hello world ZIP

Following code snippet is HTML file which contains Javascript code to generate ZIP file.





















<HTML>
<HEAD>
    <script type="text/javascript" src="jszip.js"></script>
</HEAD>
 
<BODY>
 
    <input type="button" onclick="create_zip()" value="Create Zip"></input>
 
</BODY>
<SCRIPT>
 
function create_zip() {
    var zip = new JSZip();
    zip.add("hello1.txt", "Hello First World\n");
    zip.add("hello2.txt", "Hello Second World\n");
    content = zip.generate();
    location.href="data:application/zip;base64," + content;
}
 
</SCRIPT>
</HTML>
In above code snippet, we have a button “Create Zip” which calls javascript function create_zip(). This function calls jszip API to generate ZIP file.

Create/Manipulate PDF files using JAVA

Hi Guys,

              Do you know that we can generate the PDF files on the fly in JAVA. To achieve this we need to use the iText.jar file. Just set the iText.jar file to your class path and use below program to generate a PDF file.
Knowledge Base : http://www.lowagie.com/iText/download.html.
Download Link  : http://sourceforge.net/projects/itext/files/

Sample Program
-----------------
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class GeneratePDF {
    public static void main(String[] args) {
        try {
            OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
            Document document = new Document();
            PdfWriter.getInstance(document, file);
            document.open();
            document.add(new Paragraph("Hello Kiran"));
            document.add(new Paragraph(new Date().toString()));
            document.close();
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 A Test.pdf file will be generated in " C:\\" drive.