<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2182913583834088147</atom:id><lastBuildDate>Fri, 21 Aug 2020 09:25:41 +0000</lastBuildDate><category>Java Script</category><category>Java</category><category>Interview</category><category>Plugins</category><category>jQuery</category><category>JDT</category><title>scriptSplash</title><description>my experiments with programming</description><link>http://scriptden.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-3421022005694992409</guid><pubDate>Tue, 14 Aug 2012 18:50:00 +0000</pubDate><atom:updated>2012-08-15T00:20:06.198+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Interview Questions - MultiThreading</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;This set of Java Interview Questions covers MultiThreading topics like Threads, Synchronization, DeadLock, Thread Pool and more.&lt;br /&gt;&lt;br /&gt;You can find the previous set of Interview Questions from here:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-collections.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - Collections&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-core.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - Fundamentals&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;1. What is the difference between processes and threads?&lt;/div&gt;A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.&lt;br /&gt;A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;2. Explain different ways of creating a thread? Which one is preferred?&lt;/div&gt;Threads can be used by either&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Extending the &lt;b&gt;Thread class&lt;/b&gt;.&lt;br /&gt;&lt;pre&gt;class Counter extends Thread {&lt;br /&gt;    &lt;br /&gt;    //method where the thread execution will start&lt;br /&gt;    public void run(){&lt;br /&gt;        //logic to execute in a thread   &lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    //let’s see how to start the threads&lt;br /&gt;    public static void main(String[] args){&lt;br /&gt;       Thread t1 = new Counter();&lt;br /&gt;       Thread t2 = new Counter();&lt;br /&gt;       t1.start();  //start the first thread.&lt;br /&gt;       t2.start(); //this starts the 2nd thread.&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Implementing the &lt;b&gt;Runnable interface&lt;/b&gt;.&lt;br /&gt;&lt;pre&gt;class Counter extends Base implements Runnable{&lt;br /&gt;   &lt;br /&gt;    //method where the thread execution will start&lt;br /&gt;    public void run(){&lt;br /&gt;        //logic to execute in a thread   &lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    //let us see how to start the threads&lt;br /&gt;    public static void main(String[] args){&lt;br /&gt;         Thread t1 = new Thread(new Counter());&lt;br /&gt;         Thread t2 = new Thread(new Counter());&lt;br /&gt;         t1.start();  //start the first thread.&lt;br /&gt;         t2.start();  //this starts the 2nd thread.&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;The Runnable interface is preferred, as it does not require your object to inherit a thread because when you need multiple inheritance, only interfaces can help you. &lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;3. Briefly explain high-level thread states?&lt;/div&gt;A Thread can have the below states: &lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Runnable&lt;/b&gt;: A thread becomes runnable when you call the start( ), but does  not necessarily start running immediately.  It will be pooled waiting for its turn to be picked for execution by the thread scheduler based on thread priorities.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Running&lt;/b&gt;: The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn with this static method Thread.yield( ). Because of context switching overhead, yield( ) should not be used very frequently&lt;/li&gt;&lt;li&gt;&lt;b&gt;Waiting&lt;/b&gt;: A thread is in a blocked state while it waits for some external processing such as file I/O to finish.A call to currObject.wait( ) method causes the current thread to wait until some other thread invokes currObject.notify( ) or the currObject.notifyAll( ) is executed.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Sleeping&lt;/b&gt;: Java threads are forcibly put to sleep (suspended) with this overloaded method: Thread.sleep(milliseconds), Thread.sleep(milliseconds, nanoseconds);&lt;/li&gt;&lt;li&gt;&lt;b&gt;Blocked &lt;/b&gt;:Will move to runnable after chnage in I/O condition or when lock is acquired.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Dead&lt;/b&gt;: The thread is finished working.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;4. What is multithreading and what are the methods for inter-thread communication?&lt;/div&gt;Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class.&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;wait()&lt;/b&gt; : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state.&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;notify()&lt;/b&gt; or &lt;b&gt;notifyAll()&lt;/b&gt; : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;5. What&#39;s the difference between the methods sleep() and wait()?&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;The code sleep(1000); puts thread aside for exactly one second whereas the code wait(1000), causes a wait of up to one second.&lt;/li&gt;&lt;li&gt;A thread could stop waiting earlier if it receives the notify() or notifyAll() call.&lt;/li&gt;&lt;li&gt;The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;6. Why wait, notify, and notifyall methods are defined in the Object class, and not in the Thread class?&lt;/div&gt;Every Java Object has a monitor associated with it. The threads using that object can lock or unlock the monitor associated with the object. Wait and notify/notifyAll methods are responsible for acquiring and relinquishing the lock associated with the particular object. Calling wait causes the current thread to wait to acquire the lock of the Object, and calling notify/notifyAll relinquishes the lock and notify the threads waiting for that lock.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;7. What do you understand by Synchronization?&lt;/div&gt;Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object&#39;s value. Synchronization prevents such type of data corruption.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;E.g. Synchronizing a function:&lt;/u&gt;&lt;br /&gt;&lt;pre&gt;public synchronized void Method1 () {&lt;br /&gt;    // Appropriate method-related code. &lt;br /&gt;}&lt;/pre&gt;&lt;u&gt;E.g. Synchronizing a block of code inside a function:&lt;/u&gt;&lt;br /&gt;&lt;pre&gt;public myFunction (){&lt;br /&gt;    synchronized (this) { &lt;br /&gt;    // Synchronized code here.&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;8. What are the disadvantages of synchronization?&lt;/div&gt;The disadvantage of synchronize is it will end up in slowing down the program. Also if not handled properly it will end up in dead lock.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;9. Why would you use a synchronized block vs. synchronized method?&lt;/div&gt;Synchronized blocks place locks for shorter periods than synchronized methods.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;10. When you will synchronize a piece of your code?&lt;/div&gt;When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;11. What is a ThreadLocal class?&lt;/div&gt;ThreadLocal is a handy class for simplifying development of thread-safe concurrent programs by making the object stored in this class not sharable between threads.&lt;br /&gt;Below are some key points about ThreadLocal variables :&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;A thread-local variable effectively provides a separate copy of its value for each thread that uses it.&lt;/li&gt;&lt;li&gt;ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread&lt;/li&gt;&lt;li&gt;In case when multiple threads access a ThreadLocal instance, separate copy of Threadlocal variable is maintained for each thread.&lt;/li&gt;&lt;li&gt;Common use is seen in DAO pattern where the DAO class can be singleton but the Database connection can be maintained separately for each thread. (Per Thread Singleton)&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. What is deadlock?&lt;/div&gt;When two threads are waiting each other and can’t precede the program is said to be deadlock. Deadlock may occur when two threads, each having a lock on the same resource, attempt to acquire a lock on the other&#39;s resource. Each thread would wait indefinitely for the other resource to release the lock, unless one of the user processes is terminated.&lt;br /&gt;The thread deadlock can occur in conditions such as:&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;two threads calling Thread.join() on each other.&lt;/li&gt;&lt;li&gt;two threads use nested synchronized blocks to lock two objects and blocks lock the same objects in different order.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. What is daemon thread and which method is used to create the daemon thread?&lt;/div&gt;Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. What is immutable object? How does it help in writing concurrent application?&lt;/div&gt;An object is considered immutable if its state cannot change after it is constructed. Immutable objects are particularly useful in concurrent applications, since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state. Examples of immutable objects from the JDK include String and Integer.&lt;br /&gt;Immutable objects greatly simplify your multi threaded program, since they are&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Simple to construct, test, and use.&lt;/li&gt;&lt;li&gt;Automatically thread-safe and have no synchronization issues.&lt;/li&gt;&lt;/ul&gt;To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;15. What is Starvation? and What is a Livelock?&lt;/div&gt;Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;LiveLock &lt;/b&gt;: Livelock occurs when all threads are blocked, or are otherwise unable to proceed due to unavailability of required resources, and the non-existence of any unblocked thread to make those resources available. In terms of Java API, thread livelock can occur in following conditions:&lt;br /&gt;&lt;ol&gt;&lt;li&gt; When all the threads in a program execute Object.wait(0) on an object with zero parameter. The program is live-locked and cannot proceed until one or more threads call Object.notify() or Object.notifyAll() on the relevant objects. Because all the threads are blocked, neither call can be made.&lt;/li&gt;&lt;li&gt; When all the threads in a program are stuck in infinite loops.&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Starvation &lt;/b&gt;: Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by &quot;greedy&quot; threads. In Java, thread starvation can be caused by setting thread priorities inappropriately. A lower-priority thread can be starved by higher-priority threads if the higher-priority threads do not yield control of the CPU from time to time.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;16. What is thread pool? Why should we use thread pools?&lt;/div&gt;A thread pool is a collection of threads on which task can be scheduled. Instead of creating a new thread for each task, you can have one of the threads from the thread pool pulled out of the pool and assigned to the task. When the thread is finished with the task, it adds itself back to the pool and waits for another assignment.&lt;br /&gt;One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread.&lt;br /&gt;Below are key reasons to use a Thread Pool :&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Using thread pools minimizes the JVM overhead due to thread creation.&lt;/li&gt;&lt;li&gt;You have control over the maximum number of tasks that are being processed in parallel (= number of threads in the pool).&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/08/java-interview-questions-multithreading.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-7539279998017780811</guid><pubDate>Sat, 11 Aug 2012 16:11:00 +0000</pubDate><atom:updated>2012-08-14T23:02:54.190+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Interview Questions - Exception</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is a set of Java Interview Questions which focuses on Exceptions and the Exception Handling framework. You can find the previous set of Interview Questions from here:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-collections.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - Collections&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-core.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - Fundamentals&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;1. What is an exception?&lt;/div&gt;An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program&#39;s instructions.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;2. How does exception handling work in Java?&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;It separates the working/functional code from the error-handling code by way of try-catch clauses. &lt;/li&gt;&lt;li&gt;It allows a clean path for error propagation. If the called method encounters a situation it can’t manage, it can throw an exception and let the calling method deal with it. &lt;/li&gt;&lt;li&gt;By enlisting the compiler to ensure that &quot;exceptional&quot; situations are anticipated and accounted for, it enforces powerful coding. &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;3. What are the different ways to generate an Exception?&lt;/div&gt;There are two different ways to generate an Exception:&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Exceptions can be generated by the Java run-time system. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment.&lt;/li&gt;&lt;li&gt;Exceptions can be manually generated by your code.Manually generated exceptions are typically used to report some error condition to the caller of a method.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;4. What are the two types of Exception in Java?&lt;/div&gt;Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions and Runtime exceptions, or unchecked exceptions.&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Compiler-enforced&lt;/b&gt; (checked) exceptions are instances of the Exception class or one of its subclasses — excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Runtime &lt;/b&gt;exception represent unexpected exceptional conditions which can be handled but not necessarily recovered from. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;5. Explain the User-defined or Custom Exceptions?&lt;/div&gt;User defined Exceptions are the separate Exception classes defined by the user for specific purpose. An user defined exception can be created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.&lt;br /&gt;Example:&lt;br /&gt;&lt;pre&gt;class myCustomException extends Exception {&lt;br /&gt;       // The class simply has to exist to be an exception&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;6. Does it matter in what order catch statements for FileNotFoundException and IOException are written?&lt;/div&gt;Yes, it does. The FileNoFoundException is inherited from the IOException. Exception&#39;s subclasses have to be caught first.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;7. What is error?&lt;/div&gt;An Error indicates that a non-recoverable condition has occurred that should not be caught. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;8. What is the difference between exception and error?&lt;/div&gt;The &lt;b&gt;exception &lt;/b&gt;class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file which does not exist, when the network connection is disrupted, or when operands being manipulated are out of prescribed ranges.&lt;br /&gt;The &lt;b&gt;error &lt;/b&gt;class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;9. What is the super class of java.lang.Exception&lt;/div&gt;java.lang.Throwable, the parent class of all exception related classes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;10. Explain the significance of try-catch blocks?&lt;/div&gt;Whenever the exception occurs in Java, we need a way to tell the JVM what code to execute. To do this, we use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. One or more catch clauses match a specific exception to a block of code that handles it.&lt;br /&gt;Eg:&lt;br /&gt;&lt;pre&gt;try {&lt;br /&gt;   // try block for code for which we want to catch exceptions.&lt;br /&gt;} catch (Exception e) {&lt;br /&gt;   // catch block to handle the exception.&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;11. What is the use of finally block?&lt;/div&gt;The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.&lt;br /&gt;If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. What if there is a break or return statement in try block followed by finally block?&lt;/div&gt;If there is a return statement in the try block, the finally block executes right after the return statement encountered, and before the return executes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. Can we have the try block without catch block?&lt;/div&gt;Yes, we can have the try block without catch block, but finally block should follow the try block.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. What is the difference throw and throws?&lt;/div&gt;&lt;b&gt;throws&lt;/b&gt;: Used in a method&#39;s signature if a method is capable of causing an exception that it does not handle, so that callers of the method can guard themselves against that exception. If a method is declared as throwing a particular class of exceptions, then any other method that calls it must either have a try-catch clause to handle that exception or must be declared to throw that exception (or its superclass) itself.&lt;br /&gt;&lt;pre&gt;public void someMethod(g) throws someException {&lt;br /&gt;      // Method body&lt;br /&gt;}&lt;/pre&gt;&lt;b&gt;throw&lt;/b&gt;: Used to trigger an exception. The exception will be caught by the nearest try-catch clause that can catch that type of exception. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed.&lt;br /&gt;&lt;pre&gt;throw new someException(&quot;Catch this one !!&quot;);&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/08/java-interview-questions-exception.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-4851702056218801492</guid><pubDate>Thu, 02 Aug 2012 19:16:00 +0000</pubDate><atom:updated>2012-08-03T00:47:07.284+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Core Java Interview Questions - Part 3</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is the Third set of Core Java Interview Questions. You can find the previous set of interview questions from below : &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-core.html&quot; target=&quot;_blank&quot;&gt;Core Java Interview Questions - Part 1&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/08/core-java-interview-questions-part-2.html&quot; target=&quot;_blank&quot;&gt;Core Java Interview Questions - Part 2&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;1. Where and how can you use a private constructor.&lt;/div&gt;Private constructor can be used if you do not want any other class to instanstiate the object , the instantiation is done from a static public method. This method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;2. Can a top level class be private or protected?&lt;/div&gt;No. A top level class can not be private or protected. It can have either &quot;public&quot; or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the &quot;modifier private is not allowed here&quot;. This means that a top level class can not be private. Same is the case with protected.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;3. Differentiate between continue and break.&lt;/div&gt;In break, control comes out of the loop whereas in continue control stops at “continue” statement. The remaining statements aren’t executed as control enters following loop.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;4. What restrictions are placed on method overriding in Java Programming?&lt;/div&gt;Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;5. Why equals should be overridden?&lt;/div&gt;Two objects are considered equal if referring to the same object as the equals method uses only the operator &lt;b&gt;== &lt;/b&gt;to compare. Hence while using the object as keys, equals() method needs to be overridden. Similarly while using hashmaps or hashtables the method hashcode needs to be overridden as the object is placed in the hash using hashcode value.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;6. Can we override a static method?&lt;/b&gt;&lt;/div&gt;The static methods cannot be overridden. If a subclass defines a static method with the same signature as a static method in the superclass, the method in the subclass hides the one in the superclass.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;7. What are Transient and Volatile Modifiers?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Transient: &lt;/b&gt;Transient keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).&lt;br /&gt;&lt;b&gt;Volatile: &lt;/b&gt;Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;8. What is Garbage Collection?&lt;/b&gt;&lt;/div&gt;When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as &lt;b&gt;garbage collection.&lt;/b&gt;&lt;br /&gt;Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can&#39;t directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;9. How you can force the garbage collection?&lt;/b&gt;&lt;/div&gt;Garbage collection automatic process and can&#39;t be forced. You could request it by calling &lt;b&gt;System.gc()&lt;/b&gt; or &lt;b&gt;Runtime.gc()&lt;/b&gt;. JVM does not guarantee that GC will be started immediately and when all the objects will garbage collected.&lt;br /&gt;Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;10. What are immutable objects in Java? Can we change the value of an immutable object?&lt;/b&gt;&lt;/div&gt;An object is considered to be immutable if its value cannot change after it is created. Immutable objects are particularly very useful in multi-threaded applications. Since they cannot change state, they won’t get corrupted by thread interference or observed in an inconsistent state. Objects  of java.lang.Integer and java.lang.String classes are the examples of immutable objects. To create an immutable object You need to make the class final and all its members private or final, so that once objects gets created no one can modify its state.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;11. What do you understand by numeric promotion?&lt;/div&gt;The Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In the numerical promotion process the byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. What is an applet?&lt;/div&gt;Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser. Applet needs no explicit installation on local machine and runs itself automatically in a java-enabled browser.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. What is the lifecycle of an applet?&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;init() method - Can be called when an applet is first loaded&lt;/li&gt;&lt;li&gt;start() method - Can be called each time an applet is started.&lt;/li&gt;&lt;li&gt;paint() method - Can be called when the applet is minimized or maximized.&lt;/li&gt;&lt;li&gt;stop() method - Can be used when the browser moves off the applet’s page.&lt;/li&gt;&lt;li&gt;destroy() method - Can be called when the browser is finished with the applet.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. What is JVM (Java Virtual Machine)?&lt;/div&gt;JVM stands for Java Virtual Machine. JVM is a software implementation which stands on the top of the real hardware platform and operating system. It provides abstraction between the compiled java program and the hardware and operating system. So the compiled program does not have to worry about what hardware and operating system he has to run in, it’s all handled by the JVM and thus attaining portability. All Java programs are compiled in to bytecodes. JVM can only understand and execute Java bytecodes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;15. Explain the advantages and disadvantages of JAVA.&lt;/div&gt;JAVA offers a number of &lt;b&gt;advantages &lt;/b&gt;to developers.&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Java is simple: Java was designed to be easy to use and learn than other programming languages. Java is much simpler than C++ as it uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage.&lt;/li&gt;&lt;li&gt;Java is object-oriented: Programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.&lt;/li&gt;&lt;li&gt;Java is platform-independent: Java programs are compiled into Java Virtual Machine code called bytecode. The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform.&lt;/li&gt;&lt;li&gt;Java is distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.&lt;/li&gt;&lt;li&gt;Java is secure: Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind.&lt;/li&gt;&lt;li&gt;Java is robust: Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages.&lt;/li&gt;&lt;li&gt;Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it.&lt;/li&gt;&lt;/ol&gt;&lt;b&gt;Disadvantages &lt;/b&gt;of JAVA :&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Performance: Java can be perceived as significantly slower and more memory-consuming than natively compiled languages such as C or C++.&lt;/li&gt;&lt;li&gt;Single-paradigm language: Java is predominantly a single-paradigm language. However, with the addition of static imports in Java 5.0 the procedural paradigm is better accommodated than in earlier versions of Java.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;br /&gt;Please do comment if you find any discrepancies or want to suggest new questions.&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/08/core-java-interview-questions-part-3.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-782841130698639175</guid><pubDate>Thu, 02 Aug 2012 17:43:00 +0000</pubDate><atom:updated>2012-08-03T00:46:38.367+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Core Java Interview Questions - Part 2</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is the Second set of Core Java Interview Questions. You can find the previous set of interview questions from below : &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-core.html&quot; target=&quot;_blank&quot;&gt;Core Java Interview Questions - Part 1&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-oop.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - OOP&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;1. What would you use to compare two String variables - the operator == or the method equals()?&lt;/b&gt;&lt;/div&gt;I&#39;d use the method &lt;b&gt;equals()&lt;/b&gt; to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;2. What does the &quot;static&quot; keyword mean in front of a variable? A method? A class? Curly braces {}? &lt;/b&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;static variable:&lt;/b&gt; means a class level variable &lt;/li&gt;&lt;li&gt;&lt;b&gt;static method: &lt;/b&gt;Can be invoked even before a single instance of a class is created. It is not allowed to access the not static members of the class. &lt;/li&gt;&lt;li&gt;&lt;b&gt;static class: &lt;/b&gt;no such thing. &lt;/li&gt;&lt;li&gt;&lt;b&gt;static free floating block: &lt;/b&gt;is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using native methods.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;3. What does the &quot;final&quot; keyword mean in front of a variable? A method? A class? &lt;/b&gt;&lt;/div&gt;&lt;b&gt;FINAL for a variable : &lt;/b&gt;value is constant &lt;br /&gt;&lt;b&gt;FINAL for a method : &lt;/b&gt;final method cannot be overridden or hidden by new access specifications.&lt;br /&gt;&lt;b&gt;FINAL for a class :&lt;/b&gt; cannot be derived, The best example of a final class is the String class.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;4. What is difference between final, finalize() and finally?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;final :&lt;/b&gt; final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value. &lt;br /&gt;&lt;b&gt;finalize() :&lt;/b&gt; finalize() method is used just before an object is destroyed and can be called just prior to garbage collection. &lt;br /&gt;&lt;b&gt;finally :&lt;/b&gt; finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;5. What is casting?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Casting &lt;/b&gt;is used to convert the value of one type to another. There are two types of casting in Java, these are Implicit casting and explicit casting. Casting operation is used to convert between types and the instanceof operator is used to check for type information at run time.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;6. What are the types of casting?&lt;/div&gt;There are two types of casting in Java, these are Implicit casting and explicit casting.&lt;br /&gt;&lt;b&gt;Implicit &lt;/b&gt;casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not workout for all application scenarios.&lt;br /&gt;Example:&lt;pre&gt;int i = 4000;&lt;br /&gt;long h = i; //Implicit casting&lt;/pre&gt;&lt;b&gt;Explicit &lt;/b&gt;casting in the process in which the compiler are specifically informed to about transforming the object.&lt;br /&gt;Example:&lt;pre&gt;long ln = 700.20;&lt;br /&gt;t = (int) ln; //Explicit casting&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;7. What do you understand by downcasting?&lt;/div&gt;The process of Downcasting refers to the casting from a general to a more specific type, i.e. casting down the hierarchy.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;8. What modifiers may be used with top-level class?&lt;/b&gt;&lt;/div&gt;public, abstract and final can be used for top-level class.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;9. What are inner class and anonymous class?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Inner class :&lt;/b&gt; classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private. &lt;br /&gt;&lt;b&gt;Anonymous class :&lt;/b&gt; Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;10. Describe the wrapper classes in Java.&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Wrapper &lt;/b&gt;class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.&lt;br /&gt;eg: boolean&amp;nbsp; - java.lang.Boolean&lt;br /&gt;int - java.lang.Integer&lt;br /&gt;void - java.lang.Void&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;11. What is the difference between StringBuffer and String class?&lt;/b&gt;&lt;/div&gt;A &lt;b&gt;StringBuffer &lt;/b&gt;implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. &lt;br /&gt;The &lt;b&gt;String &lt;/b&gt;class represents character strings. All string literals in Java programs are immutable and their values cannot be changed after they are created.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. What is the difference between the boolean &#39;&lt;i&gt;&amp;amp;&lt;/i&gt;&#39; operator and the &#39;&lt;i&gt;&amp;amp;&amp;amp;&lt;/i&gt;&#39; operator?&lt;/div&gt;If an expression involving the boolean&lt;b&gt; &lt;i&gt;&amp;amp;&lt;/i&gt;&lt;/b&gt; operator is evaluated, both operands are evaluated. &lt;br /&gt;Whereas when an expression involving the &lt;b&gt;&lt;i&gt;&amp;amp;&amp;amp;&lt;/i&gt;&lt;/b&gt; operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then only the second operand is evaluated. If the first operand evaluates to false, the evaluation of the second operand is skipped.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. Why there are no global variables in Java?&lt;/div&gt;Global variables are globally accessible. Java does not support globally accessible variables due to following reasons: &lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;The global variables breaks the referential transparency&lt;/li&gt;&lt;li&gt;Global variables creates collisions in namespace.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. Why Java does not support pointers?&lt;/div&gt;Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;15. How is it possible for two String objects with identical values not to be equal under the == operator?&lt;/div&gt;The &lt;b&gt;&#39;==&#39; &lt;/b&gt;operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.&lt;/div&gt;&lt;br /&gt;Please do provide your valuable comments and suggestions. Thanks in Advance.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/08/core-java-interview-questions-part-2.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-5116652352528558291</guid><pubDate>Sun, 29 Jul 2012 06:18:00 +0000</pubDate><atom:updated>2012-08-10T18:42:52.228+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Interview Questions - Collections</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is a set of Java Interview Questions which focuses on the Collections framework. You can find the previous set of Interview Questions from here:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-oop.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - OOP&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/07/java-interview-questions-core.html&quot; target=&quot;_blank&quot;&gt;Java Interview Questions - Fundamentals&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;1. What is Collection API?&lt;/div&gt;The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces. &lt;br /&gt;Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.&lt;br /&gt;Example of interfaces: Collection, Set, List and Map.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;2. What are the classes implementing List interface?&lt;/div&gt;There are three classes that implement List interface:&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;ArrayList &lt;/b&gt;: It is a resizable array implementation. The size of the ArrayList can be increased dynamically also operations like add,remove and get can be formed once the object is created. It also ensures that the data is retrieved in the manner it was stored. The ArrayList is not thread-safe.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Vector&lt;/b&gt;: It is thread-safe implementation of ArrayList. The methods are wrapped around a synchronized block.&lt;/li&gt;&lt;li&gt;&lt;b&gt;LinkedList&lt;/b&gt;: LinkedList implements Queue interface and provide FIFO(First In First Out) operation for add operation. It is faster than ArrayList for insertion and deletion of elements from the middle of a list.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;3. Which all classes implement Set interface?&lt;/div&gt;A Set is a collection that contains no duplicate elements. HashSet, SortedSet and TreeSet are the commonly used classes which implements Set interface.&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;SortedSet &lt;/b&gt;- It is an interface which extends Set. A the name suggest , the interface allows the data to be iterated in the ascending order or sorted on the basis of Comparator or Comparable interface. All elements inserted into the interface must implement Comparable or Comparator interface.&lt;/li&gt;&lt;li&gt;&lt;b&gt;TreeSet &lt;/b&gt;- It is the implementation of SortedSet interface.This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). The class is not synchronized.&lt;/li&gt;&lt;li&gt;&lt;b&gt;HashSet&lt;/b&gt;: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;4. What is difference between List and a Set?&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;List can contain duplicate values but Set allows only unique elements.&lt;/li&gt;&lt;li&gt;List allows retrieval of data to be in same order in the way it is inserted but Set doesnt ensures the sequence in which data can be retrieved.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;5. What is difference between Arrays and ArrayList ?&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Arrays are created of fix size whereas ArrayList is of not fix size. i.e. the size of array cannot be incremented or decremented. But with arrayList the size is variable.&lt;/li&gt;&lt;li&gt;Once the array is created elements cannot be added or deleted from it. But with ArrayList the elements can be added and deleted at runtime.&lt;/li&gt;&lt;li&gt;ArrayList is one dimensional but array can be multidimensional.&lt;/li&gt;&lt;li&gt;To create an array the size should be known or initalized to some value. If not initialized carefully there could me memory wastage. But arrayList is all about dynamic creation and there is no wastage of memory.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;6. What are the main differences between a Vector and an ArrayList?&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Synchronization &lt;/b&gt;- Java Vector class is internally synchronized and ArrayList is not synchronized. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure (ArrayList) will give better performance than the synchronized one (Vector).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Data growth&lt;/b&gt; - Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;7. Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?&lt;/div&gt;The &lt;b&gt;LinkedList &lt;/b&gt;allows for fastest insertions and  deletions into the middle of a list. The LinkedList is implemented using a doubly  linked list; thus an insertion requires only the updating of the links at the point  of insertion.Whereas ArrayList and Vector both use an array to store the  elements of the list. When an element is inserted into the middle of  the list, then the elements that follow the insertion point must be shifted to make room for the new element.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;8. What is difference between HashMap and HashTable?&lt;/div&gt;Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Hashmap is not synchronized in nature but HashTable is.&lt;/li&gt;&lt;li&gt;Iterator in the HashMap is fail-safe while the enumerator for the HashTable isn&#39;t.&lt;/li&gt;&lt;li&gt;HashMap permits null values and only one null key, while HashTable doesn&#39;t allow key or value as null. &lt;/li&gt;&lt;/ol&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;9. Is Iterator a Class or Interface? What is its use?&lt;/div&gt;Iterator is an interface which is used to step through the elements of a Collection.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;10. If any ArrayList is used only for reading which way will be the faster, For loop or Iterator?&lt;/div&gt;Using a &lt;b&gt;for&lt;/b&gt; loop is faster than using iterator. A &lt;b&gt;for&lt;/b&gt; loop allows access of the element directly on the basis of index. The cursor of the datastructure can directly go to the &#39;n&#39; location and get the element. It does not traverse through n-1 elements. Wheras in Iterator based access, the cursor has to traverse through each element to get the desired element.So to reach the &#39;n&#39;th element it need to traverse through n-1 elements.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;11. When should be an Iterator preferred over a For loop?&lt;/div&gt;For loop does not allow the updation in the array(add or remove operation) inside the loop whereas Iterator does. Also Iterator can be used where the data type of the collection is not known.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. How do you traverse through a collection using its Iterator?&lt;/div&gt;To use an iterator to traverse through the contents of a collection, follow these steps:&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Obtain an iterator to the start of the collection by calling the collection’s iterator() method.&lt;/li&gt;&lt;li&gt;Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.&lt;/li&gt;&lt;li&gt;Within the loop, obtain each element by calling next().&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. How can Arraylist be synchronized without using Vector?&lt;/div&gt;Arraylist can be synchronized using:&lt;br /&gt;&lt;pre&gt;Collection.synchronizedList(List list) &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. What is identityHashMap?&lt;/div&gt;The IdentityHashMap uses == for equality checking instead of equals(). This can be used for both performance reasons, if you know that two different elements will never be equals and for preventing spoofing, where an object tries to imitate another.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;15. What are the main algorithms (methods) that the Collections framework provide?&lt;/div&gt;Most polymorphic algorithms in the Collections class apply specifically to List. Below listed are the commonly used algorithms :&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;sort — sorts a List using a merge sort algorithm, which provides a fast, stable sort. (A stable sort is one that does not reorder equal elements.)&lt;/li&gt;&lt;li&gt;shuffle — randomly permutes the elements in a List.&lt;/li&gt;&lt;li&gt;reverse — reverses the order of the elements in a List.&lt;/li&gt;&lt;li&gt;rotate — rotates all the elements in a List by a specified distance.&lt;/li&gt;&lt;li&gt;swap — swaps the elements at specified positions in a List.&lt;/li&gt;&lt;li&gt;replaceAll — replaces all occurrences of one specified value with another.&lt;/li&gt;&lt;li&gt;binarySearch — searches for an element in an ordered List using the binary search algorithm.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/java-interview-questions-collections.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-2082678885564518454</guid><pubDate>Tue, 24 Jul 2012 11:21:00 +0000</pubDate><atom:updated>2012-07-30T23:17:10.755+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java Interview Questions - OOP</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Here is the Second set of Java Interview Questions which focuses on the Object Oriented Programming principles. Please comment if you want to add any other question to this list.&lt;br /&gt;&lt;br /&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;1. What is OOP?&lt;/div&gt;OOP is the common abbreviation for Object-Oriented Programming. Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.&lt;br /&gt;There are four main principals of oops which are called Abstraction, Polymorphism, Inheritance and Encapsulation.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;2. What is the difference between procedural and object-oriented programs?&lt;/div&gt;In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.&lt;br /&gt;In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;3. What is Abstraction?&lt;/div&gt;Abstraction is the process of generalization by reducing the information content of an entity, typically to retain only information which is relevant for a particular purpose. For example, an Employee has various attributes like name, skin color, age, hair color, etc. But we consider only those attributes of employees which are required and relevant to program a system.&lt;br /&gt;In general, abstraction is the process of focusing on important thing without including background details in program.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;4. Explain the Encapsulation principle.&lt;/div&gt;Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;5. Explain the Inheritance principle.&lt;/div&gt;Inheritance is the process by which one object acquires the properties of another object. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;6. Explain the Polymorphism principle.&lt;/div&gt;The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as &quot;one interface, multiple methods&quot;. &lt;br /&gt;From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Method overloading&lt;/li&gt;&lt;li&gt;Method overriding through inheritance&lt;/li&gt;&lt;li&gt;Method overriding through the Java interface&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;7. What is the advantage of OOP?&lt;/div&gt;The Major advantages of OOP are: &lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Simplicity: &lt;/b&gt;software objects model real world objects, so the complexity is reduced and the program structure is very clear. &lt;/li&gt;&lt;li&gt;&lt;b&gt;Modularity: &lt;/b&gt;each object forms a separate entity whose internal workings are decoupled from other parts of the system.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Modifiability:&lt;/b&gt; it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Extensibility: &lt;/b&gt;adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Maintainability:&lt;/b&gt; objects can be maintained separately, making locating and fixing problems easier.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Re-usability: &lt;/b&gt;objects can be reused in different programs.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;8. What is the Difference between Class and Object?&lt;/div&gt;A Class is actually a blueprint or a template to create an Object. Whereas an Object is a an actual instance of a Class. For example Employee is a class, while John is a real employee which is an Object of Employee Class.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;9. What is method overloading and method overriding?&lt;/div&gt;&lt;b&gt;Method overloading: &lt;/b&gt;When a method in a class having the same method name with different arguments is said to be method overloading. &lt;br /&gt;&lt;b&gt;Method overriding : &lt;/b&gt;When a method in a class having the same method name with same arguments is said to be method overriding.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;10. What is difference between overloading and overriding?&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method. &lt;/li&gt;&lt;li&gt;Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass. &lt;/li&gt;&lt;li&gt;In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass. &lt;/li&gt;&lt;li&gt;Overloading must have different method signatures whereas overriding must have same signature.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;11. What is message passing in object oriented programming?&lt;/div&gt;Message passing is a method by which an object sends data to another object or requests other object to invoke method. This is also known as interfacing. It acts like a messenger from one object to other object to convey specific instructions.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;12. What are data members and member functions?&lt;/div&gt;Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost and uses functions to operate on these attributes.&lt;br /&gt;The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as methods or member functions.&lt;br /&gt;Eg: int a,b; // a,b are data members&lt;br /&gt;Void getdata ( ) ; // member function&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;13. Write the process of programming in an object-oriented language?&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Create classes that define objects and their behavior.&lt;/li&gt;&lt;li&gt;Creating objects from class definition.&lt;/li&gt;&lt;li&gt;Establishing communication among objects.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;14. What is an Abstract class?&lt;/div&gt;Abstract class defines an abstract concept which can not be instantiated and comparing o interface it can have some implementation while interfaces can not. Below are some points for abstract class:- &lt;br /&gt;We can not create object of abstract class it can only be inherited in a below class. &lt;br /&gt;Normally abstract classes have base implementation and then child classes derive from the abstract class to make the class concrete.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;15. What are Abstract methods?&lt;/div&gt;Abstract class can contain abstract methods. Abstract methods do not have implementation. Abstract methods should be implemented in the subclasses which inherit them. So if an abstract class has an abstract method class inheriting the abstract class should implement the method or else java compiler will through an error. In this way, an abstract class can define a complete programming interface thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. Abstract methods are defined using &quot;abstract&quot; keyword. Below is a sample code snippet.&lt;br /&gt;&lt;pre&gt;abstract class MyGraphics {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;abstract void draw();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Any class inheriting from &#39;MyGraphics&#39; class should implement the &#39;draw&#39; method or else the java compiler will throw an error. So if we do not implement a abstract method the program will not compile.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;16. What is the difference between Abstract classes and Interfaces?&lt;/div&gt;Difference between Abstract class and Interface is as follows:- &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Abstract class can only be inherited while interfaces can not be it has to be implemented. &lt;/li&gt;&lt;li&gt;Interface cannot implement any methods, whereas an abstract class can have implementation. &lt;/li&gt;&lt;li&gt;Class can implement many interfaces but can have only one super class. &lt;/li&gt;&lt;li&gt;Interface is not part of the class hierarchy while Abstract class comes in through inheritance.&lt;/li&gt;&lt;li&gt;Unrelated classes can implement the same interface.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;17. What is difference between Static and Non-Static fields of a class?&lt;/div&gt;Non-Static values are also called as instance variables. Each object of the class has its own copy of Non-Static instance variables. So when a new object is created of the same class it will have completely its own copy of instance variables.&lt;br /&gt;Static values have only one copy of instance variables and will be shared among all the objects of the class.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;18. What are inner classes and what is the practical implementation of inner classes?&lt;/div&gt;Inner classes are nested inside other class. They have access to outer class fields and methods even if the fields of outer class are defined as private.&lt;br /&gt;Normally inner classes are used for data structures or some kind of helper classes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;19. What is a constructor in class?&lt;/div&gt;Constructor has the same name as the class in which it resides and looks from syntax point of view it looks similar to a method. Constructor is automatically called immediately after the object is created, before the new operator completes. Constructors have no return type, not even void. This is because the implicit return type of a class&#39; constructor is the class type itself. It is the constructor&#39;s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;20. Can constructors be parameterized?&lt;/div&gt;Yes we can have parameterized constructor which can also be termed as constructor overloading. Below is a code snippet which shows two constructors for MathsPI class one default (parameter-less) and other with parameter.&lt;br /&gt;&lt;pre&gt;class MathsPI {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;double PI;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// This is the constructor for the maths constant class.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;MathsPI() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PI = 3.14;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;MathsPI(int pi) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PI = pi;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;21. What are access modifiers?&lt;/div&gt;Access modifiers decide whether a method or a data variable can be accessed by another method in another class or subclass.&lt;br /&gt;four types of access modifiers:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Public - Can be accessed by any other class anywhere.&lt;/li&gt;&lt;li&gt;Protected - Can be accessed by classes inside the package or by subclasses ( that means classes who inherit from this class).&lt;/li&gt;&lt;li&gt;Private - Can be accessed only within the class. Even methods in subclasses in the same package do not have access.&lt;/li&gt;&lt;li&gt;Default - (Its private access by default) accessible to classes in the same package but not by classes in other packages, even if these are subclasses. &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;22. What is runtime polymorphism or dynamic method dispatch?&lt;/div&gt;In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;23. What is Dynamic Binding?&lt;/div&gt;Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;24. How do you prevent a method from being overridden?&lt;/div&gt;To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means &quot;this is the final implementation of this method&quot;, the end of its inheritance hierarchy.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;25. Do interfaces have member variables?&lt;/div&gt;Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments.&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/java-interview-questions-oop.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-6193043896869056838</guid><pubDate>Mon, 23 Jul 2012 18:00:00 +0000</pubDate><atom:updated>2012-08-02T23:51:15.680+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interview</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Core Java Interview Questions - Part 1</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Had an interview the other day for a java opening and just compiled a list of frequently asked Java Interview questions for different specializations. Thought of sharing the lists for the benefit of all those who are attending or planning for a Java Interview.&lt;br /&gt;&lt;br /&gt;This post focuses on Interview questions for the &lt;b&gt;Java Fundamentals&lt;/b&gt; i.e the core functionality. If you feel that some important questions are missed or you want suggest a different answer to any Interview question, do comment and I will be glad to update on it.&lt;br /&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;1. What are Class, Constructor and Primitive data types?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Class &lt;/b&gt;is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. &lt;br /&gt;&lt;b&gt;Constructor &lt;/b&gt;is a special kind of method that determines how an object is initialized when created.&lt;br /&gt;&lt;b&gt;Primitive &lt;/b&gt;data types are 8 types and they are: byte, short, int, long, float, double, boolean, char. &lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;2. What is an Object and describe what happens when an object is created in Java?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Object &lt;/b&gt;is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.&lt;br /&gt;Several things happen in a particular order to ensure the object is constructed properly: &lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implementation-specific data includes pointers to class and method data. &lt;/li&gt;&lt;li&gt;The instance variables of the objects are initialized to their default values. &lt;/li&gt;&lt;li&gt;The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java. &lt;/li&gt;&lt;li&gt;Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;3. What is the difference between constructor and method?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Constructor &lt;/b&gt;will be automatically invoked when an object is created whereas &lt;b&gt;method &lt;/b&gt;has to be called explicitly.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;4. What are methods and how are they defined?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Methods &lt;/b&gt;are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes. &lt;br /&gt;Method definition has four parts. They are :&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;name of the method&lt;/li&gt;&lt;li&gt;type of object or primitive type the method returns&lt;/li&gt;&lt;li&gt;a list of parameters&lt;/li&gt;&lt;li&gt;body of the method.&lt;/li&gt;&lt;/ul&gt;A method’s signature is a combination of the first three parts mentioned above.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;5. How many ways can an argument be passed to a subroutine and explain them?&lt;/b&gt;&lt;/div&gt;An argument can be passed in two ways. They are passing by value and passing by reference. &lt;br /&gt;&lt;b&gt;Passing by value:&lt;/b&gt; This method copies the value of an argument into the formal parameter of the subroutine.&lt;br /&gt;&lt;b&gt;Passing by reference:&lt;/b&gt; In this method, a reference to an argument (not the value of the argument) is passed to the parameter. &lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;6. What is the difference between this() and super()?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;this() &lt;/b&gt;can be used to invoke a constructor of the same class. Use this() to call a constructor from another constructor.&lt;br /&gt;&lt;b&gt;super()&lt;/b&gt; can be used to invoke a super class constructor. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass&#39;s constructor.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;7. Explain the usage of Java packages.&lt;/b&gt;&lt;/div&gt;This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;8. If a class is located in a package, what do you need to change in the OS environment to be able to use it?&lt;/b&gt;&lt;/div&gt;You need to add a directory or a jar file that contains the package directories to the &lt;b&gt;CLASSPATH&lt;/b&gt; environment variable.&lt;br /&gt;Let&#39;s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you&#39;d need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:&lt;br /&gt;c:\&amp;gt;java com.xyz.hr.Employee&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;9. What&#39;s the difference between constructors and normal methods?&lt;/b&gt;&lt;/div&gt;&lt;b&gt;Constructors &lt;/b&gt;must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;10. What is the difference between superclass and subclass?&lt;/b&gt;&lt;/div&gt;A super class is a class that is inherited whereas sub class is a class that does the inheriting.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;11. What does the &quot;abstract&quot; keyword mean in front of a method? A class? &lt;/b&gt;&lt;/div&gt;If a method has a abstract keyword in front of it,it is called abstract method. Abstract method has no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses. &lt;br /&gt;Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract.&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;12. How to define an Abstract class?&lt;/b&gt;&lt;/div&gt;A class containing abstract method is called Abstract class. An Abstract class can&#39;t be instantiated.&lt;br /&gt;Example of Abstract class:&lt;br /&gt;&lt;pre&gt;abstract class testAbstractClass { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected String myString; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getMyString() { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return myString; &lt;br /&gt;} &lt;br /&gt;public abstract string anyAbstractFunction();&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;13. How to define an Interface?&lt;/b&gt;&lt;/div&gt;In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.&lt;br /&gt;Example of Interface:&lt;br /&gt;&lt;pre&gt;public interface sampleInterface {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void functionOne();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public long CONSTANT_ONE = 1000;&lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;14. What are the advantages of interface?&lt;/b&gt;&lt;/div&gt;Interfaces are useful for: &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Declaring methods that one or more classes are expected to implement &lt;/li&gt;&lt;li&gt;Capturing similarities between unrelated classes without forcing a class relationship.&lt;/li&gt;&lt;li&gt;Determining an object’s programming interface without revealing the actual body of the class.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;QandA&quot;&gt;&lt;div id=&quot;Question&quot;&gt;&lt;b&gt;15. What&#39;s the difference between an interface and an abstract class? Also discuss the similarities. (Very Important)&lt;/b&gt;&lt;/div&gt;Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn&#39;t contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods&lt;br /&gt;&lt;br /&gt;Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Differences:&lt;/b&gt;&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Interfaces provide a form of multiple inheritance. A class can extend only one other class.&lt;/li&gt;&lt;li&gt;Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.&lt;/li&gt;&lt;li&gt;A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.&lt;/li&gt;&lt;li&gt;Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast. &lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Similarities:&lt;/b&gt;&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Neither Abstract classes or Interface can be instantiated.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;You can find the next set of Core Java Interview Questions from below : &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://www.scriptsplash.com/2012/08/core-java-interview-questions-part-2.html&quot; target=&quot;_blank&quot;&gt;Core Java Interview Questions - Part 2&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/java-interview-questions-core.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-4670819648911829462</guid><pubDate>Sat, 21 Jul 2012 17:28:00 +0000</pubDate><atom:updated>2012-07-24T17:00:59.857+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">jQuery</category><category domain="http://www.blogger.com/atom/ns#">Plugins</category><title>The Best Free jQuery Plugins</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;To be successful as a blogger, you definitely need a helping hand with  some of the best free jQuery plugins which can help you leverage your  efforts. I have listed here some best free jQuery plugins of different functions like slider, layout, form validation, table, etc. Please feel free to suggest any new ones in the comment section.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.josscrowcroft.com/projects/motioncaptcha-jquery-plugin/&quot; target=&quot;_blank&quot;&gt;MotionCaptcha - Innovative Captcha Tool&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-HPlOQLBRf48/UAqCa6L0nkI/AAAAAAAAAGg/95ktm3ioaPM/s1600/motion_captcha.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;173&quot; src=&quot;http://4.bp.blogspot.com/-HPlOQLBRf48/UAqCa6L0nkI/AAAAAAAAAGg/95ktm3ioaPM/s320/motion_captcha.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;MotionCAPTCHA is a jQuery CAPTCHA plugin, based on the HTML5 Canvas Harmony procedural drawing tool, requiring users to sketch the shape they see in the canvas in order to submit a form.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.egrappler.com/jquery-strong-password-plugin-power-pwchecker/&quot; target=&quot;_blank&quot;&gt;Power PWChecker - jQuery password strength checker&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-MapmDChFLk0/UAp_zfm-S7I/AAAAAAAAAGQ/eonWcI7yPjM/s1600/password-strength-checker.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;123&quot; src=&quot;http://1.bp.blogspot.com/-MapmDChFLk0/UAp_zfm-S7I/AAAAAAAAAGQ/eonWcI7yPjM/s320/password-strength-checker.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Power PWChecker is a free jQuery plugin which ensures secure passwords for users. This simple jQuery plugin can be integrated seamlessly with any online form (sign up form/ user registration form) to check password strength and give users clues for creating strong and secure passwords.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://tablesorter.com/docs/&quot; target=&quot;_blank&quot;&gt;tablesorter - Sortable Table&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-lLp5DDnfmhQ/UAqAxvv5mMI/AAAAAAAAAGY/LlkQ8ASphXg/s1600/tablesorter.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;122&quot; src=&quot;http://2.bp.blogspot.com/-lLp5DDnfmhQ/UAqAxvv5mMI/AAAAAAAAAGY/LlkQ8ASphXg/s320/tablesorter.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;tablesorter is a jQuery plugin for turning a standard HTML table with THEAD and TBODY tags into a sortable table without page refreshes. tablesorter can successfully parse and sort many types of data including linked data in a cell. It has many useful features including Multi-column sorting and&amp;nbsp; Multi-type sorting.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://fancybox.net/&quot; target=&quot;_blank&quot;&gt;FancyBox - Image Gallery Box&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://3.bp.blogspot.com/-CYYHAoBQKRw/UAqEio39yiI/AAAAAAAAAGo/XVz7Ap51Iaw/s1600/fancyBox.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;217&quot; src=&quot;http://3.bp.blogspot.com/-CYYHAoBQKRw/UAqEio39yiI/AAAAAAAAAGo/XVz7Ap51Iaw/s320/fancyBox.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;FancyBox is a tool for displaying images, html content and multi-media in a Mac-style &quot;lightbox&quot; that floats overtop of web page. It can display images, HTML elements, SWF movies, Iframes and also Ajax requests. It is customizable through settings and CSS and supports various fancy transitions.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.egrappler.com/a-stylo-modern-jquery-accordion-akordeon/&quot; target=&quot;_blank&quot;&gt;Akordeon - Stylish jQuery Accordion Plugin&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-NwDC_xJRS5o/UApKmL6_pEI/AAAAAAAAAGA/iAcwPgWB8Zo/s1600/akordeon-modern-jquery-accordion-plugin.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;213&quot; src=&quot;http://4.bp.blogspot.com/-NwDC_xJRS5o/UApKmL6_pEI/AAAAAAAAAGA/iAcwPgWB8Zo/s320/akordeon-modern-jquery-accordion-plugin.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Akordeon is a stylish jQuery plugin for adding efficient accordion style menu to a web page. The idea behind Akordeon is to provide a lightweight and customizable interface for collapsible panels that can hold any kind of data in a limited space.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://nivo.dev7studios.com/&quot; target=&quot;_blank&quot;&gt;Nivo Slider - Awesome jQuery slider&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-9b9uEgG7Ksg/UAqIl9QEsgI/AAAAAAAAAHA/mF7W52IKgJ0/s1600/nivo_slider.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;139&quot; src=&quot;http://1.bp.blogspot.com/-9b9uEgG7Ksg/UAqIl9QEsgI/AAAAAAAAAHA/mF7W52IKgJ0/s320/nivo_slider.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;The Nivo Slider is world renowned as the most beautiful and easy to use slider on the market. Completely free and totally open source, there literally is no better way to make your website look totally stunning.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.steamdev.com/snippet/&quot; target=&quot;_blank&quot;&gt;Snippet - jQuery Syntax Highlighter&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-xbStPzBHoDo/UAqIk1SQCpI/AAAAAAAAAG4/m2RA6tdcR9c/s1600/jQuery_snippet.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;179&quot; src=&quot;http://2.bp.blogspot.com/-xbStPzBHoDo/UAqIk1SQCpI/AAAAAAAAAG4/m2RA6tdcR9c/s320/jQuery_snippet.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Snippet is a jQuery syntax highlighting plugin built on top of the SHJS script found on SourceForge. Snippet provides a quick and easy way of highlighting source code passages in HTML documents.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.mind-projects.it/projects/jqzoom/&quot; target=&quot;_blank&quot;&gt;jQZoom - Javascript image magnifier&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://3.bp.blogspot.com/-61taoDcBtRU/UAmifVMyIpI/AAAAAAAAAFk/3lCaLqVRX8g/s1600/pjpteysue-15.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;http://3.bp.blogspot.com/-61taoDcBtRU/UAmifVMyIpI/AAAAAAAAAFk/3lCaLqVRX8g/s320/pjpteysue-15.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;JQZoom is a javascript image magnifier built at the top of the popular jQuery javascript framework. jQzoom is a great and a really easy to use script to magnify what you want. It works on all modern browsers.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.inwebson.com/jquery/jqfloat-js-a-floating-effect-with-jquery/&quot; target=&quot;_blank&quot;&gt;jqFloat - Floating Effect with jQuery&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-Obe7_QwR4Ho/UAmiea13wXI/AAAAAAAAAFc/wOt4WLuZn9M/s1600/pjpteysue-12.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;http://2.bp.blogspot.com/-Obe7_QwR4Ho/UAmiea13wXI/AAAAAAAAAFc/wOt4WLuZn9M/s320/pjpteysue-12.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;jqFloat.js is a jQuery plugin that able to make any HTML objects appear to be “floating” on your web page. It helps create simple floating animation and make your websites come alive with these little “floating” object.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.uploadify.com/&quot; target=&quot;_blank&quot;&gt;Uploadify - Multiple File Upload jQuery Plugin&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-0iwKmX5mThc/UAmigUFNL7I/AAAAAAAAAFs/UDGzj3cMe4k/s1600/pjpteysue-49.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;http://1.bp.blogspot.com/-0iwKmX5mThc/UAmigUFNL7I/AAAAAAAAAFs/UDGzj3cMe4k/s320/pjpteysue-49.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Uploadify is a jQuery plugin that integrates a fully-customizable multiple file upload utility on your website. It uses a mixture of JavaScript, ActionScript, and any server-side language to dynamically create an instance over any DOM element on a page.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;https://github.com/elclanrs/jq-idealforms&quot; target=&quot;_blank&quot;&gt;IdealForms - jQuery Form Plugin&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-eoTdaGX_CWY/UAqJY5XR9hI/AAAAAAAAAHI/sQYxUfQkkAs/s1600/ideal-forms.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;205&quot; src=&quot;http://4.bp.blogspot.com/-eoTdaGX_CWY/UAqJY5XR9hI/AAAAAAAAAHI/sQYxUfQkkAs/s320/ideal-forms.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Ideal Forms is the ultimate framework for building and validating responsive HTML5 forms. It is fully responsive, has keyboard support, input customization, custom datepicker and &#39;On the spot&#39; validation.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.minimit.com/mg/demo.html&quot; target=&quot;_blank&quot;&gt;Minimit - jQuery Gallery Plugin&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://3.bp.blogspot.com/-ExZxKiKm0mk/UArepUvm7BI/AAAAAAAAAHY/GB2LyGIGybY/s1600/minimit_skider.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;160&quot; src=&quot;http://3.bp.blogspot.com/-ExZxKiKm0mk/UArepUvm7BI/AAAAAAAAAHY/GB2LyGIGybY/s400/minimit_skider.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Minimit Gallery is the most custom slider plugin you’ll ever find. It’s tested for and supports Css3 transition and transform, drags, scrollers, and touch interactions.It is tested on IE7+, Firefox 3.5+, Safari 3+, Opera 9+, Chrome, Iphone, Ipad, Android.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://js.recurly.com/&quot; target=&quot;_blank&quot;&gt;Recurly - jQuery Forms With Fully Customizable CSS&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-gjkBZ9tlAaA/UArhaBFWT2I/AAAAAAAAAHw/K5rh33fxun8/s1600/recurly.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;92&quot; src=&quot;http://1.bp.blogspot.com/-gjkBZ9tlAaA/UArhaBFWT2I/AAAAAAAAAHw/K5rh33fxun8/s400/recurly.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Recurly.js is a Javascript library designed to be easily embedded and customized to match your website. The library performs in-line validation, real-time total calculations, and gracefully handles errors. Your customer stays on your website while their billing information is securely sent to Recurly for approval.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;http://www.woothemes.com/flexslider/&quot; target=&quot;_blank&quot;&gt;Flexslider - Fully responsive jQuery slider plugin.&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-bI9gQt5I1vU/UArhYxN73KI/AAAAAAAAAHo/C3MqpybjgFA/s1600/flex_slider.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;223&quot; src=&quot;http://2.bp.blogspot.com/-bI9gQt5I1vU/UArhYxN73KI/AAAAAAAAAHo/C3MqpybjgFA/s320/flex_slider.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;FlexSlider is a fully responsive jQuery slider plugin. FlexSlider has been verified in Safari 4+, Chrome 4+, Firefox 3.6+, Opera 10+, and IE7+. iOS and Android devices are supported as well.&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/the-best-free-jquery-plugins.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-HPlOQLBRf48/UAqCa6L0nkI/AAAAAAAAAGg/95ktm3ioaPM/s72-c/motion_captcha.JPG" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-7565604480417500194</guid><pubDate>Tue, 10 Jul 2012 17:31:00 +0000</pubDate><atom:updated>2012-07-24T17:01:19.462+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">JDT</category><category domain="http://www.blogger.com/atom/ns#">Plugins</category><title>Create a JDT Project in Eclipse</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JDT stands for Java development tools, the sub-project of the Eclipse project that develops tools for programming in Java. This includes a Java compiler, incremental builder, editors, wizards, content assist, and all the other features of a first class Java IDE.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The JDT Core component is the plug-in that defines the core Java elements and API. This Core component can be included inside our own plugin and can be used to search, compile and manipulate Java code outside an IDE. This is because the JDT/Core infrastructure has no built-in JDK version dependencies, and it  also does not depend on any particular Java UI.&lt;br /&gt;&lt;br /&gt;Below are some of the activities that can be performed by using the JDT Core plug-in :&lt;br /&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Programmatically manipulate Java resources, such as creating projects, generating Java source code, performing builds, or detecting problems in code.&lt;/li&gt;&lt;li&gt;Searching the workspace&#39;s Java model for Java elements that match a particular description.&lt;/li&gt;&lt;li&gt;Generating Abstract Syntax Trees (AST) that can be used for examining the structure of a compilation unit down to the statement level.&lt;/li&gt;&lt;li&gt;Formatting of compilation units, types, statements, expressions, etc.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JDT is supported by 3 pillars: Java Model, Search Engine, and AST. More info about JDT can be found from &lt;a href=&quot;http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Foverview-summary.html&quot;&gt;here&lt;/a&gt;. Here is couple of nice presentations which briefly describes all JDT fundamentals and features - &lt;a href=&quot;http://www.slideshare.net/oliviert/jdt-fundamentals-2010&quot;&gt;JDT Fundamentals 2010&lt;/a&gt; and &lt;a href=&quot;http://www.eclipsecon.org/2008/sub/attachments/JDT_fundamentals.ppt&quot;&gt;JDT Fundamentals&lt;/a&gt;. JDT allows you to access Java Code using&amp;nbsp; AST (Abstract Syntax Tree). AST is a Document Object Model for Java, where each code element is represented as AST Nodes. It gives you a detailed tree structureof the Java source code. Some of the ASTNodes are MethodDeclaration, FieldDeclaration,  MethodInvocation, VariableDeclarationStatement, ExpressionStatement etc. In this post we will be using this AST functionality to retrieve information from a Java Source File.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; At first we need to create a plug-in project which uses JDT Core plugin. Instructions on creating a plug-in project in eclipse can be found from &lt;a href=&quot;http://www.scriptsplash.com/2012/06/create-plug-in-project-in-eclipse.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. Name the new project JDT_Basic. Now to use the JDT core functionality we need to add it as a plug-in to  our project. We can do this from the &#39;&lt;i&gt;&lt;b&gt;Dependencies&lt;/b&gt;&lt;/i&gt;&#39; tab (view this tab by opening the plugin.xml file). Click on the  &#39;Add&#39; button and add the &lt;i&gt;&lt;b&gt;&#39;org.eclipse.jdt.core&#39;&lt;/b&gt;&lt;/i&gt; plug-in. This way we  have included the JDT Core plug-in our project and thus will be able to use the AST functionalities.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&amp;nbsp;&lt;a href=&quot;http://4.bp.blogspot.com/-9FgM_UUiS2U/T_xlPsn0AXI/AAAAAAAAAFA/GgKhYt4S0JE/s1600/jdt2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;243&quot; src=&quot;http://4.bp.blogspot.com/-9FgM_UUiS2U/T_xlPsn0AXI/AAAAAAAAAFA/GgKhYt4S0JE/s400/jdt2.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For running this plugin project we will be using the &#39;&lt;i&gt;&lt;b&gt;applications&lt;/b&gt;&lt;/i&gt;&#39; extension. For this, double click on the plugin.xml file and open the &#39;&lt;i&gt;&lt;b&gt;Extensions&lt;/b&gt;&lt;/i&gt;&#39; tab. Remove the &#39;actionSets&#39; extension and add the &#39;applications&#39; extension (org.eclipse.core.runtime.applications). Expand the extension in the window and create a new run by right clicking and choosing &lt;i&gt;&lt;b&gt;New &amp;gt; run&lt;/b&gt;&lt;/i&gt;. Specify the class as &#39;jdt_basic.JDTClass&#39; and click on the &#39;class&#39; label to create the Class.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-ZBIVNcNAPW0/T_xlNYIsLKI/AAAAAAAAAE4/H4rwK3x4H94/s1600/jdt1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;230&quot; src=&quot;http://1.bp.blogspot.com/-ZBIVNcNAPW0/T_xlNYIsLKI/AAAAAAAAAE4/H4rwK3x4H94/s400/jdt1.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This class will be used to invoke our JDT application. The &#39;&lt;i&gt;&lt;b&gt;start&lt;/b&gt;&lt;/i&gt;&#39; method of the application class denotes the starting point of the plug-in application, whereas the &#39;&lt;i&gt;&lt;b&gt;stop&lt;/b&gt;&lt;/i&gt;&#39; method is invoked at the end of the application. Using this application class we will try to implement a simple activity, to list all the methods in a Java file.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Here is how we are going to accomplish this. At first we will read the content of the Java file using a String Buffer. Using this content(source) we will create a CompilationUnit for the Java file. This CompilationUnit gives you an AST representation of the Java File. To traverse the various AST nodes of this CompilationUnit you can use the ASTVisitor pattern. In this example we will list out the Class methods by traversing the &lt;a href=&quot;http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Foverview-summary.html&quot; target=&quot;_blank&quot;&gt;MethodDeclaration &lt;/a&gt;node.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Here is the code for the JDTClass. Each step in the code has documented with comments for clear understanding. If you have any questions or queries, please comment. I will be more than happy to help you out.&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;package jdt_basic;&lt;br /&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.File;&lt;br /&gt;import java.io.FileNotFoundException;&lt;br /&gt;import java.io.FileReader;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import org.eclipse.equinox.app.IApplication;&lt;br /&gt;import org.eclipse.equinox.app.IApplicationContext;&lt;br /&gt;import org.eclipse.jdt.core.dom.AST;&lt;br /&gt;import org.eclipse.jdt.core.dom.ASTParser;&lt;br /&gt;import org.eclipse.jdt.core.dom.ASTVisitor;&lt;br /&gt;import org.eclipse.jdt.core.dom.CompilationUnit;&lt;br /&gt;import org.eclipse.jdt.core.dom.MethodDeclaration;&lt;br /&gt;&lt;br /&gt;/** &lt;br /&gt; * This class provides basic JDT functionality.&lt;br /&gt;*/&lt;br /&gt;public class JDTClass implements IApplication {&lt;br /&gt;    &lt;br /&gt;    /** &lt;br /&gt;     * The starting point of the plug-in application.&lt;br /&gt;     * Visit the Code specified as path and list the methods inside the class.&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    public Object start(IApplicationContext context) throws Exception {&lt;br /&gt;&lt;br /&gt;        // Get the source code of the java class file - change path to your file&#39;s path.&lt;br /&gt;        final StringBuffer sourceCode = getSource(&quot;d:\\HelloWorld.java&quot;);&lt;br /&gt;        // Define a new ASTParser.&lt;br /&gt;        ASTParser parser = ASTParser.newParser(AST.JLS3);&lt;br /&gt;        // Parse the class as a compilation unit.&lt;br /&gt;        parser.setKind(ASTParser.K_COMPILATION_UNIT);&lt;br /&gt;        // Supply the class code as character array.&lt;br /&gt;        parser.setSource(sourceCode.toString().toCharArray());&lt;br /&gt;        parser.setResolveBindings(true);&lt;br /&gt;        // Get the compiled class as a compilation unit  &lt;br /&gt;        final CompilationUnit compiledClassUnit = (CompilationUnit) parser.createAST(null);&lt;br /&gt;        // Instantiate a new ASTVisitor class that will determine which nodes are visited during an AST traversal&lt;br /&gt;        compiledClassUnit.accept(new ASTVisitor() {&lt;br /&gt;            @Override&lt;br /&gt;            // Visit the MethodDeclaration node - denotes the declaration of methods in a class&lt;br /&gt;            public boolean visit(MethodDeclaration node) {&lt;br /&gt;                // Show the visited method in console.&lt;br /&gt;                System.out.println(&quot;Method &quot; + node.getName().getFullyQualifiedName() + &quot; is visited&quot;);&lt;br /&gt;                return true;&lt;br /&gt;            }&lt;br /&gt;        });&lt;br /&gt;        return IApplication.EXIT_OK;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Returns the source code of the java class file as string buffer.&lt;br /&gt;     * @param path the path of the Java class file&lt;br /&gt;     * @return StringBuffer string buffer containing the source code&lt;br /&gt;     */&lt;br /&gt;    private static StringBuffer getSource(String path) {&lt;br /&gt;&lt;br /&gt;        // Define string buffer for holding the source&lt;br /&gt;        final StringBuffer buffer = new StringBuffer();&lt;br /&gt;        try {&lt;br /&gt;            // Read the file line by line and append to string buffer.&lt;br /&gt;            File javaFile = new File(path);&lt;br /&gt;            BufferedReader in = new BufferedReader(new FileReader(javaFile)); &lt;br /&gt;            String line = null;&lt;br /&gt;            while (null != (line = in.readLine())) {&lt;br /&gt;                buffer.append(line).append(&quot;\n&quot;);&lt;br /&gt;            }&lt;br /&gt;        } catch (FileNotFoundException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        } catch (IOException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        }&lt;br /&gt;        return buffer;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /** &lt;br /&gt;     * The end point of the plug-in application.&lt;br /&gt;     */&lt;br /&gt;    public void stop() {&lt;br /&gt;        // TODO Auto-generated method stub&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/create-jdt-project-in-eclipse.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-9FgM_UUiS2U/T_xlPsn0AXI/AAAAAAAAAFA/GgKhYt4S0JE/s72-c/jdt2.JPG" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-5191975421669179290</guid><pubDate>Wed, 04 Jul 2012 15:16:00 +0000</pubDate><atom:updated>2012-07-24T16:56:49.924+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Chained DropDown in Javascript using XML</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-bETzN4StvkU/UA1qECcVNiI/AAAAAAAAAIg/2G_oAt_5KcA/s1600/cascade.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://4.bp.blogspot.com/-bETzN4StvkU/UA1qECcVNiI/AAAAAAAAAIg/2G_oAt_5KcA/s1600/cascade.JPG&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Sample Chained HTML DropDown&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;In our previous &lt;a href=&quot;http://www.scriptsplash.com/2012/07/chained-dropdown-in-javascript.html&quot;&gt;post &lt;/a&gt;we have learned about how to create Chained Drop-downs using javascript. In that example, the values for the 2nd drop-down were being hard-coded in the javascript itself. Though this approach is fairly easy to implement, it doen&#39;t offer a fully dynamic approach. If we need to add/delete options or update any exisitng options, then we need to alter the javascript code each time. Also a person who has less or no previous experience with Javascript may not be able to use or modify that logic.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This problem can be solved by externalizing your data from the code, say, in form of an XML. In order to change the drop-down values you just need to add, delete or modify the values in the XML file. In this post we will discuss how to implement such a dynamic Chained DropDown using javascript.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; We will create the two drop-downs, a &#39;Company&#39; drop-down which lists mobile companies and &#39;Model&#39; drop-down which list out models for the selected company.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;select id=&quot;Company&quot; &amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;&quot;&amp;gt;&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Samsung&quot;&amp;gt;Samsung&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Apple&quot;&amp;gt;Apple&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Nokia&quot;&amp;gt;Nokia&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;select id=&quot;Model&quot;&amp;gt; &amp;lt;/select&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XML stands for Extensible Markup Language. It is classified as an extensible language because it allows its users to define their own tags. The XML file which has our data has a &#39;device&#39; tag containing a &#39;name&#39; attribute which holds the Company name. The model tag is contained inside a device tag and holds information about the Model name. Here is the content of the &#39;info.xml&#39; file which holds the data for this example.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; ?&amp;gt; &lt;br /&gt;&amp;lt;data&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;device name=&quot;Nokia&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;Lumia&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;PureView&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/device&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;device name=&quot;Samsung&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;Galaxy S2&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;Galaxy S3&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/device&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;device name=&quot;Apple&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;iPhone4&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;model&amp;gt;iPhone5&amp;lt;/model&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/device&amp;gt;&lt;br /&gt;&amp;lt;/data&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now we need to import this data from the XML file. To manipulate an XML document in javascript, we need an XML parser. Today all browsers come with in-built parsers that can parse the XML document. The parser loads the document into your computer’s memory and once loaded, its data can be manipulated using the DOM(Document Object Model). We are using a cross-browser function to import the XML as there is significant difference in implementation of Microsoft Browser based XML parser and the Mozilla browsers (Firefox, Chrome, Safari) based XML parser.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;/* Cross-Browser Import of XML document into a XML Document. */&lt;br /&gt;function importXML(xmlfile) {&lt;br /&gt;    var xmlDoc;&lt;br /&gt;    if (typeof window.ActiveXObject != &#39;undefined&#39;) {&lt;br /&gt;        //code for IE&lt;br /&gt;        xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);&lt;br /&gt;        xmlDoc.async = false;&lt;br /&gt;        xmlDoc.load(xmlfile);&lt;br /&gt;        } else {&lt;br /&gt;            try {&lt;br /&gt;                // code for IE7+, Firefox, Chrome, Opera, Safari&lt;br /&gt;                xmlhttp=new XMLHttpRequest();&lt;br /&gt;                xmlhttp.open(&quot;GET&quot;, xmlfile, false);&lt;br /&gt;                xmlhttp.setRequestHeader(&#39;Content-Type&#39;, &#39;text/xml&#39;);&lt;br /&gt;                xmlhttp.send();&lt;br /&gt;                xmlDoc=xmlhttp.responseXML;&lt;br /&gt;            } catch (Exception) {&lt;br /&gt;                alert(&quot;Your browser is not supported. Try firefox !!&quot;);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    return xmlDoc;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The loadXMLOption method retrieves the options for the model combobox from the XML file. This is to be called when user selects an option in the &#39;Company&#39; drop-down. It first loads the XML file by using the &#39;importXML&#39; method we have previously written. From the XML document object, we retrieve all the tags with tag name as &#39;device&#39;. We then loop through each &#39;device&#39; tag and check if its &#39;name&#39; attribute&#39;s value is same as the one we have selected in the &#39;Company&#39; drop-down. If found similar, we then retrieve all the &#39;model&#39; tags under that specific &#39;device&#39; tag and add each tag&#39;s node value as option to the &#39;Model&#39; drop-down.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;/* Get the options for the model combobox from the XML. */&lt;br /&gt;function loadXMLOption() {&lt;br /&gt;    // Load the xml file&lt;br /&gt;    var xmlDoc=importXML(&quot;info.xml&quot;);&lt;br /&gt;    xmlObj=xmlDoc.documentElement;&lt;br /&gt;    // Get all Elements with Tag name &#39;device&#39;&lt;br /&gt;    DeviceObj = xmlObj.getElementsByTagName(&quot;device&quot;);&lt;br /&gt;    for(var i=0; i &amp;lt; DeviceObj.length; i++) {&lt;br /&gt;        // Loop through each device tag and check if its name is equal to the selected device value&lt;br /&gt;        if (document.getElementById(&#39;Company&#39;).value == DeviceObj[i].getAttribute(&quot;name&quot;)) {  &lt;br /&gt;            // If matching device found get the model tags under that device&lt;br /&gt;            modelObj = DeviceObj[i].getElementsByTagName(&quot;model&quot;);&lt;br /&gt;            document.getElementById(&#39;Model&#39;).options.length = 0;&lt;br /&gt;            // Create options for the Model comboBox.&lt;br /&gt;            for(var j=0; j &amp;lt; modelObj.length; j++) {&lt;br /&gt;                var opt = document.createElement(&#39;option&#39;);&lt;br /&gt;                opt.value = modelObj[j].firstChild.nodeValue;&lt;br /&gt;                opt.text = modelObj[j].firstChild.nodeValue;&lt;br /&gt;                document.getElementById(&#39;Model&#39;).options.add(opt);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Now call this method on the onChange event of the company drop-down. &lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;&amp;lt;select id=&quot;Company&quot; onchange=&quot;loadXMLOption();&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thats it and now we have a truly dynamic Chained Drop-downs. You can change or add the model values in the XML file. If you want an easy way to develop chained drop-down without involving XML, you can try this &lt;a href=&quot;http://www.scriptsplash.com/2012/07/chained-dropdown-in-javascript.html&quot;&gt;post&lt;/a&gt;.&lt;br /&gt;You can get the entire code from below. Comments and suggestions are welcome.  &lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;script language=&quot;javascript&quot;&amp;gt;&lt;br /&gt;/* Cross-Browser Import of XML document into a XML Document. */&lt;br /&gt;function importXML(xmlfile) {&lt;br /&gt;    var xmlDoc;&lt;br /&gt;    if (typeof window.ActiveXObject != &#39;undefined&#39;) {&lt;br /&gt;        //code for IE&lt;br /&gt;        xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);&lt;br /&gt;        xmlDoc.async = false;&lt;br /&gt;        xmlDoc.load(xmlfile);&lt;br /&gt;        } else {&lt;br /&gt;            try {&lt;br /&gt;                // code for IE7+, Firefox, Chrome, Opera, Safari&lt;br /&gt;                xmlhttp=new XMLHttpRequest();&lt;br /&gt;                xmlhttp.open(&quot;GET&quot;, xmlfile, false);&lt;br /&gt;                xmlhttp.setRequestHeader(&#39;Content-Type&#39;, &#39;text/xml&#39;);&lt;br /&gt;                xmlhttp.send();&lt;br /&gt;                xmlDoc=xmlhttp.responseXML;&lt;br /&gt;            } catch (Exception) {&lt;br /&gt;                alert(&quot;Your browser is not supported. Try firefox !!&quot;);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    return xmlDoc;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Get the options for the model combobox from the XML. */&lt;br /&gt;function loadXMLOption() {&lt;br /&gt;    // Load the xml file&lt;br /&gt;    var xmlDoc=importXML(&quot;info.xml&quot;);&lt;br /&gt;    xmlObj=xmlDoc.documentElement;&lt;br /&gt;    // Get all Elements with Tag name &#39;device&#39;&lt;br /&gt;    DeviceObj = xmlObj.getElementsByTagName(&quot;device&quot;);&lt;br /&gt;    for(var i=0; i &amp;lt; DeviceObj.length; i++) {&lt;br /&gt;        // Loop through each device tag and check if its name is equal to the selected device value&lt;br /&gt;        if (document.getElementById(&#39;Company&#39;).value == DeviceObj[i].getAttribute(&quot;name&quot;)) {  &lt;br /&gt;            // If matching device found get the model tags under that device&lt;br /&gt;            modelObj = DeviceObj[i].getElementsByTagName(&quot;model&quot;);&lt;br /&gt;            document.getElementById(&#39;Model&#39;).options.length = 0;&lt;br /&gt;            // Create options for the Model comboBox.&lt;br /&gt;            for(var j=0; j &amp;lt; modelObj.length; j++) {&lt;br /&gt;                var opt = document.createElement(&#39;option&#39;);&lt;br /&gt;                opt.value = modelObj[j].firstChild.nodeValue;&lt;br /&gt;                opt.text = modelObj[j].firstChild.nodeValue;&lt;br /&gt;                document.getElementById(&#39;Model&#39;).options.add(opt);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;h2&amp;gt;Chained Drop-Down - using XML&amp;lt;/h2&amp;gt;&lt;br /&gt;&lt;br /&gt;Device : &amp;lt;select id=&quot;Company&quot; onchange=&quot;loadXMLOption();&quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;option value=&quot;&quot;&amp;gt;&amp;lt;/option&amp;gt; &lt;br /&gt;&amp;nbsp; &amp;lt;option value=&quot;Samsung&quot;&amp;gt;Samsung&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;option value=&quot;Apple&quot;&amp;gt;Apple&amp;lt;/option&amp;gt; &lt;br /&gt;&amp;nbsp; &amp;lt;option value=&quot;Nokia&quot;&amp;gt;Nokia&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&lt;br /&gt;Model : &amp;lt;select id=&quot;Model&quot;&amp;gt; &amp;lt;/select&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/chained-dropdown-in-javascript-using-xml.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-bETzN4StvkU/UA1qECcVNiI/AAAAAAAAAIg/2G_oAt_5KcA/s72-c/cascade.JPG" height="72" width="72"/><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-3378847748704901523</guid><pubDate>Tue, 03 Jul 2012 14:55:00 +0000</pubDate><atom:updated>2012-07-24T17:01:34.119+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Chained DropDown in Javascript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;span id=&quot;goog_1337013646&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_1337013647&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-O7dEcOrTMDo/UA1uQyjAhrI/AAAAAAAAAI4/rU-hsQ-6reE/s1600/cascade1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://4.bp.blogspot.com/-O7dEcOrTMDo/UA1uQyjAhrI/AAAAAAAAAI4/rU-hsQ-6reE/s1600/cascade1.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;You might have seen chained drop-downs in some sites, where changing a value in one drop-down causes new values to be populated in another drop-down. This chain effect can be simply implemented using Javascript.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; At first, we will create the two drop-downs for which we are implementing the chained effect. We will provide the options in the 1st drop-down. No options need to be given for the 2nd drop-down and it values will be populated dynamically based upon the value of the 1st drop-down. We have named the first drop-down &#39;Company&#39; which lists some mobile companies. The second drop-down &#39;Model&#39; will list out few models for the selected company.&lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;&amp;lt;select id=&quot;Company&quot; &amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;&quot;&amp;gt;&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Samsung&quot;&amp;gt;Samsung&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Apple&quot;&amp;gt;Apple&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Nokia&quot;&amp;gt;Nokia&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;select id=&quot;Model&quot;&amp;gt; &amp;lt;/select&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now on to the coding part. The &lt;i&gt;&lt;b&gt;configureDropDown &lt;/b&gt;&lt;/i&gt;method is fired on the change of value in the Company drop-down and populates the values in the &#39;Model&#39; drop-down. We will first define the arrays containing the value of models for each company. Now we will check the value of the Company drop-down and based on it get the values for the Model drop-down.&lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;/* Configure the DropDown ddl2 based on the value selected in DropDown ddl1. */&lt;br /&gt;function configureDropDown(ddl1,ddl2) {&lt;br /&gt;&lt;br /&gt; // Arrays holding the value for ddl2 (model comboBox).&lt;br /&gt; var Nokia = new Array(&#39;Lumia&#39;, &#39;PureView&#39;);&lt;br /&gt; var Samsung = new Array(&#39;Galaxy S3&#39;, &#39;Galaxy S2&#39;);&lt;br /&gt; var Apple = new Array(&#39;iPhone4&#39;, &#39;iPhone5&#39;);&lt;br /&gt;  &lt;br /&gt; // Check the value in ddl1 (Company comboBox).&lt;br /&gt; switch (ddl1.value) {&lt;br /&gt;  case &#39;Samsung&#39;:&lt;br /&gt;   getOptionsFromArray(Samsung, document.getElementById(ddl2));&lt;br /&gt;   break;&lt;br /&gt;  case &#39;Apple&#39;:&lt;br /&gt;   getOptionsFromArray(Apple, document.getElementById(ddl2));&lt;br /&gt;   break;&lt;br /&gt;  case &#39;Nokia&#39;:                 &lt;br /&gt;   getOptionsFromArray(Nokia, document.getElementById(ddl2));&lt;br /&gt;   break;&lt;br /&gt;  default:&lt;br /&gt;   document.getElementById(ddl2).options.length = 0;&lt;br /&gt;   break; &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The &lt;i&gt;&lt;b&gt;getOptionsFromArray &lt;/b&gt;&lt;/i&gt;method retrieves the options for the model combobox from the respective array. The method loops through the entire array and adds the array values as options to the drop-down.&lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;// Get the options for the model combobox from the respective array. &lt;br /&gt;function getOptionsFromArray(selectArray, select) {&lt;br /&gt; select.options.length = 0;&lt;br /&gt; // Create option for the comboBox.&lt;br /&gt; for (i = 0; i &amp;lt; selectArray.length; i++) {&lt;br /&gt;        var opt = document.createElement(&#39;option&#39;);&lt;br /&gt;  opt.value = selectArray[i];&lt;br /&gt;  opt.text = selectArray[i];&lt;br /&gt;  select.options.add(opt);&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Now call this method on the &lt;i&gt;&lt;b&gt;onChange &lt;/b&gt;&lt;/i&gt;event of the company drop-down.&lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;&amp;lt;select id=&quot;Company&quot; onchange=&quot;configureDropDown(this,&#39;Model&#39;)&quot;&amp;gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This finishes our tutorial. We now have a &#39;Model&#39; drop-down whose values changes depending on the value of Company drop-down. You can get the entire code from below. &lt;br /&gt;&lt;pre class=&quot;prettyprint &quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;     /* Configure the DropDown ddl2 based on the value selected in DropDown ddl1. */&lt;br /&gt;     function configureDropDown(ddl1,ddl2) {&lt;br /&gt;&lt;br /&gt; // Arrays holding the value for ddl2 (model comboBox).&lt;br /&gt; var Nokia = new Array(&#39;Lumia&#39;, &#39;PureView&#39;);&lt;br /&gt; var Samsung = new Array(&#39;Galaxy S3&#39;, &#39;Galaxy S2&#39;);&lt;br /&gt; var Apple = new Array(&#39;iPhone4&#39;, &#39;iPhone5&#39;);&lt;br /&gt;  &lt;br /&gt; // Check the value in ddl1 (Company comboBox).&lt;br /&gt; switch (ddl1.value) {&lt;br /&gt;      case &#39;Samsung&#39;:&lt;br /&gt;           getOptionsFromArray(Samsung, document.getElementById(ddl2));&lt;br /&gt;           break;&lt;br /&gt;      case &#39;Apple&#39;:&lt;br /&gt;           getOptionsFromArray(Apple, document.getElementById(ddl2));&lt;br /&gt;           break;&lt;br /&gt;      case &#39;Nokia&#39;:                 &lt;br /&gt;           getOptionsFromArray(Nokia, document.getElementById(ddl2));&lt;br /&gt;           break;&lt;br /&gt;      default:&lt;br /&gt;           document.getElementById(ddl2).options.length = 0;&lt;br /&gt;           break; &lt;br /&gt; }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;     // Get the options for the model combobox from the respective array. &lt;br /&gt;     function getOptionsFromArray(selectArray, select) {&lt;br /&gt; select.options.length = 0;&lt;br /&gt; // Create option for the comboBox.&lt;br /&gt; for (i = 0; i &amp;lt; selectArray.length; i++) {&lt;br /&gt;        var opt = document.createElement(&#39;option&#39;);&lt;br /&gt;      opt.value = selectArray[i];&lt;br /&gt;      opt.text = selectArray[i];&lt;br /&gt;      select.options.add(opt);&lt;br /&gt; }&lt;br /&gt;     }&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;h2&amp;gt;Chained Drop-Downs&amp;lt;/h2&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;select id=&quot;Company&quot; onchange=&quot;configureDropDown(this,&#39;Model&#39;)&quot;&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;&quot;&amp;gt;&amp;lt;/option&amp;gt; &lt;br /&gt; &amp;lt;option value=&quot;Samsung&quot;&amp;gt;Samsung&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&quot;Apple&quot;&amp;gt;Apple&amp;lt;/option&amp;gt; &lt;br /&gt; &amp;lt;option value=&quot;Nokia&quot;&amp;gt;Nokia&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;select id=&quot;Model&quot;&amp;gt; &amp;lt;/select&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;Your comments and suggestions are welcome. &lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/07/chained-dropdown-in-javascript.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-O7dEcOrTMDo/UA1uQyjAhrI/AAAAAAAAAI4/rU-hsQ-6reE/s72-c/cascade1.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-8227122347417080453</guid><pubDate>Wed, 27 Jun 2012 19:42:00 +0000</pubDate><atom:updated>2012-07-24T17:02:06.442+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Plugins</category><title>Create Plug-in project in Eclipse</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In this post we will learn how to create a plug-in project in Eclipse using the plug-in development environment (PDE).&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; At its simplest, a plug-in project requires a MANIFEST.MF file describing the plugin and its dependencies and, if extending the workbench a plugin.xml file identifying the extension points being implemented, and a set of Java classes implementing those extension points.&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;b&gt;1. &lt;/b&gt;The first step involves opening your Eclipse workspace and creating a new Plug-in project.You can do it from Select File-&amp;gt;New-&amp;gt;Project. Then in the opening dialog box select the &lt;b&gt;Plug-in Project&lt;/b&gt; wizard. Then Click &lt;b&gt;Next&lt;/b&gt;.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-H4kJ1mthWfg/T-cxRP0BH_I/AAAAAAAAADQ/NBNwHY8JF68/s1600/img1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://4.bp.blogspot.com/-H4kJ1mthWfg/T-cxRP0BH_I/AAAAAAAAADQ/NBNwHY8JF68/s1600/img1.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;&lt;b&gt;2. &lt;/b&gt;Enter the project name of your choice. Leave the other options as default and click &lt;b&gt;Next.&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-Fwl7CQlKBJQ/T-cxUGwDl4I/AAAAAAAAADg/pKDy41cPf-E/s1600/img2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-Fwl7CQlKBJQ/T-cxUGwDl4I/AAAAAAAAADg/pKDy41cPf-E/s1600/img2.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;3. &lt;/b&gt;Again no changes to the default options and click &lt;b&gt;Next&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-9D9nYKR04Co/T-cxVtdZIuI/AAAAAAAAADo/0jgCoJ17aow/s1600/img3.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-9D9nYKR04Co/T-cxVtdZIuI/AAAAAAAAADo/0jgCoJ17aow/s1600/img3.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;4.&lt;/b&gt; In the next dialog&amp;nbsp; select the&lt;b&gt; Hello World &lt;/b&gt;template and press the &lt;b&gt;                 &lt;span class=&quot;guilabel&quot;&gt;Finish&lt;/span&gt;                  &lt;/b&gt;button. This                  should automatically open the                  &lt;span class=&quot;guilabel&quot;&gt;Plug-in                      Development                     &lt;/span&gt;                  perspective.                              &lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-2cjypUuAq8U/T-cxXFMJRkI/AAAAAAAAADw/SxRqDRiDihU/s1600/img4.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-2cjypUuAq8U/T-cxXFMJRkI/AAAAAAAAADw/SxRqDRiDihU/s1600/img4.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;5. &lt;/b&gt;Your new plugin project is created with the Plug-in Development perspective. This is how our plug-in project will look in the Package Explorer.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-dNI6Dz7jqto/T-cxYIB7j0I/AAAAAAAAAD4/dLXoUW9wTmw/s1600/img5.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-dNI6Dz7jqto/T-cxYIB7j0I/AAAAAAAAAD4/dLXoUW9wTmw/s1600/img5.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now lets discuss about the plug-in related files we see in our Project explorer. If you have created a java project before then you would be knowing about the build.properties file. For those who are not familair with java project, the build.properties file declares the libraries where the plug-in code is    packaged. At runtime, the class loader searches these libraries when    loading the plug-in&#39;s classes.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The &lt;i&gt;&lt;b&gt;manifest &lt;/b&gt;&lt;/i&gt;file describes the content of the plug-in to the Eclipse  runtime. In addition to basic plug-in information such as plug-in  identifier, version, etc., this file contains four main sections:  Dependencies, Extensions, Runtime and Extension Points section.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-3DjdISMdDfA/T-cxclxK27I/AAAAAAAAAEQ/SoNlz5OHMSk/s1600/img8.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://4.bp.blogspot.com/-3DjdISMdDfA/T-cxclxK27I/AAAAAAAAAEQ/SoNlz5OHMSk/s1600/img8.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;i&gt;&lt;b&gt;Dependencies &lt;/b&gt;&lt;/i&gt;section lists all the plug-ins required by the plug-in. A plug-in must list as dependencies all the plug-ins needed for its code to compile and all the plug-ins contributing extension points that it is using.&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&amp;nbsp;&lt;a href=&quot;http://4.bp.blogspot.com/-cKMiTj_HRRE/T-cxZS1PJeI/AAAAAAAAAEA/8SpNncDhYtA/s1600/img6.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-nMzHzo6iXZM/T-cxS9lVRQI/AAAAAAAAADY/rc8eEFx08bo/s1600/img10.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;255&quot; src=&quot;http://4.bp.blogspot.com/-nMzHzo6iXZM/T-cxS9lVRQI/AAAAAAAAADY/rc8eEFx08bo/s400/img10.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The &lt;i&gt;&lt;b&gt;Extensions &lt;/b&gt;&lt;/i&gt;section declares all the functionalities that this plug-in contributes to the platform by extending other plug-ins&#39; extension points.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-imZOC0zE8uI/T-cxd6aI8gI/AAAAAAAAAEY/nvSJJjoUSJE/s1600/img9.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;219&quot; src=&quot;http://2.bp.blogspot.com/-imZOC0zE8uI/T-cxd6aI8gI/AAAAAAAAAEY/nvSJJjoUSJE/s400/img9.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-H4kJ1mthWfg/T-cxRP0BH_I/AAAAAAAAADQ/NBNwHY8JF68/s1600/img1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The &lt;i&gt;&lt;b&gt;plugin.xml&lt;/b&gt;&lt;/i&gt; specifies the extension. It is the central place for  configuring the plugin being developed. This sample plugin’s extension  point is org.eclipse.ui.actionSets.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-CP-73AKMAW8/T-cxawqcewI/AAAAAAAAAEI/OsyHxjO2qYA/s1600/img7.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-CP-73AKMAW8/T-cxawqcewI/AAAAAAAAAEI/OsyHxjO2qYA/s1600/img7.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Your plugin is created and you are ready to go. Now you only need to add the code to implement the extension points. &lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;In our coming tutorial we will learn how to create a working JDT plugin project using Eclipse.&lt;br /&gt;Comments and Suggestions are appreciated.&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/06/create-plug-in-project-in-eclipse.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-H4kJ1mthWfg/T-cxRP0BH_I/AAAAAAAAADQ/NBNwHY8JF68/s72-c/img1.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-4100268498666174516</guid><pubDate>Fri, 22 Jun 2012 19:55:00 +0000</pubDate><atom:updated>2012-07-27T18:22:42.487+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Calendar using Javascript (with css)</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-aTxkekx83oQ/T-THiD2H0uI/AAAAAAAAADE/HIF5n_bQ24c/s1600/cal1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-aTxkekx83oQ/T-THiD2H0uI/AAAAAAAAADE/HIF5n_bQ24c/s1600/cal1.JPG&quot; /&gt;&lt;/a&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; This post discusses on how to make a simple, elegant monthly calendar using javascript. You can change the appearance of the calendar by simply changing the CSS to suit your needs. The calendar displays the current days of the month and highlights the current day.&lt;br /&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This is how our calendar will look after we complete this post. You can easily change the calendar appearance by modifying this css,  everything from calendar dimensions, colors, spacing, down to the font used to  highlight the current day. My aim is to present an basic understanding of date manipulations and thus this post will not cover the advanced options like moving between next and previous months.&lt;br /&gt;&lt;br /&gt;Now lets move onto the javascript code details.&amp;nbsp; &lt;br /&gt;First we will define some constant variables which we will require in our code.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;// Array to store month names&lt;br /&gt;var months = new Array(&#39;January&#39;,&#39; February&#39;, &#39;March&#39;, &#39;April&#39;, &#39;May&#39;, &#39;June&#39;, &#39;July&#39;, &#39;August&#39;, &#39;September&#39;, &#39;October&#39;, &#39;November&#39;, &#39;December&#39;); &lt;br /&gt;&lt;br /&gt;// Array to store month days&lt;br /&gt;var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);&lt;br /&gt;&lt;br /&gt;// Array to store week names&lt;br /&gt;var weekDay = new Array(&#39;Mo&#39;, &#39;Tu&#39;, &#39;We&#39;, &#39;Th&#39;, &#39;Fr&#39;, &#39;Sa&#39;, &#39;Su&#39;);&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Next we will get the current date properties by instantiating a &#39;Date&#39; object. The below code gets the current date, month, year and the number of days for current month.&lt;/span&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;var date = new Date();&lt;br /&gt;&lt;br /&gt;var day = date.getDate();&lt;br /&gt;var month = date.getMonth();&lt;br /&gt;var year = date.getFullYear();&lt;br /&gt;var days_in_this_month = monthDays[month];&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then get the week-day for the 1st day of the month. We will require this to add blank columns at the beginning in our calendar table.&lt;/span&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;var first_week_day = new Date(year, month, 1).getDay();&lt;br /&gt;&lt;/pre&gt;Now as we have set up all the variables, on to the code for displaying the calendar. The comments in the code will help you understand the logic of each statement.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;// Define the variable which holds the calendar table&lt;br /&gt;&amp;nbsp;&amp;nbsp;var calendar_html = &#39;&amp;lt;table class=&quot;calendarTable&quot;&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Create row for displaying current month and year&lt;br /&gt;&amp;nbsp;&amp;nbsp;calendar_html += &#39;&amp;lt;tr&amp;gt;&amp;lt;td class=&quot;monthHead&quot; colspan=&quot;7&quot;&amp;gt;&#39; + months[month] + &#39; &#39; + year + &#39;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&#39;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;calendar_html += &#39;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Create row to display the week days (Monday - Sunday)&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(week_day= 0; week_day &amp;lt; 7; week_day++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    calendar_html += &#39;&amp;lt;td class=&quot;weekDay&quot;&amp;gt;&#39; + weekDay[week_day] + &#39;&amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;calendar_html += &#39;&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Fill first row of the month with empty cells until the month starts.&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(week_day = 0; week_day &amp;lt; first_week_day; week_day++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    calendar_html += &#39;&amp;lt;td&amp;gt; &amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Populate the days in the month&lt;br /&gt;&amp;nbsp;&amp;nbsp;week_day = first_week_day;&lt;br /&gt;&amp;nbsp;&amp;nbsp;for(day_counter = 1; day_counter &amp;lt;= days_in_this_month; day_counter++) {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    week_day %= 7;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    // Check if week ends, if yes move to next row&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    if(week_day == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;        calendar_html += &#39;&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    // Show the current day in bold.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    if(day == day_counter)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    calendar_html += &#39;&amp;lt;td class=&quot;currentDay&quot;&amp;gt;&#39; + day_counter + &#39;&amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    calendar_html += &#39;&amp;lt;td class=&quot;monthDay&quot;&amp;gt; &#39; + day_counter + &#39; &amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    week_day++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Close the Calendar table&lt;br /&gt;&amp;nbsp;&amp;nbsp;calendar_html += &#39;&amp;lt;/tr&amp;gt;&#39;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;calendar_html += &#39;&amp;lt;/table&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// Display the calendar.&lt;br /&gt;&amp;nbsp;&amp;nbsp;document.write(calendar_html);&lt;br /&gt;&lt;/pre&gt;Now on to styling the calendar. The following css styles will give a simple style to your calendar. You can easily change the calendar appearance by modifying this css, everything from calendar dimensions, colors, down to the font used to highlight the current day.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;.calendarTable {&lt;br /&gt;    background-color:#eee;&lt;br /&gt;    color:#333;&lt;br /&gt;    border: 1px solid #bbb;&lt;br /&gt;}&lt;br /&gt;.calendarTable td {&lt;br /&gt;    text-align: center;&lt;br /&gt;    padding: 2px 4px 2px 4px;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.monthHead {&lt;br /&gt;    font-weight: bold;&lt;br /&gt;    border: 1px solid #bbb;&lt;br /&gt;    background-color:#ddd;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.weekDay {&lt;br /&gt;    font-weight: bold;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.monthDay {&lt;br /&gt;    border: 1px solid #ddd;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.currentDay {&lt;br /&gt;    font-weight: bold;&lt;br /&gt;    color:#ad5;&lt;br /&gt;    border: 1px solid #aaa;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;We have finished designing our basic calendar. If you have any question let me know in the comments. You can find the entire code along with html below.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;www.scriptSplash.com =&amp;gt; javaScript - Basic Calendar&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;style type=&quot;text/css&quot;&amp;gt;&lt;br /&gt;.calendarTable {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;background-color:#eee;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;color:#333;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;border: 1px solid #bbb;&lt;br /&gt;}&lt;br /&gt;.calendarTable td {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;text-align: center;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;padding: 2px 4px 2px 4px;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.monthHead {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;font-weight: bold;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;border: 1px solid #bbb;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;background-color:#ddd;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.weekDay {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;font-weight: bold;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.monthDay {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;border: 1px solid #ddd;&lt;br /&gt;}&lt;br /&gt;.calendarTable td.currentDay {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;font-weight: bold;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;color:#ad5;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;border: 1px solid #aaa;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&amp;lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&amp;gt;&lt;br /&gt;/** Function to display a Calendar based on javascript. **/&lt;br /&gt;function calendar() {&lt;br /&gt;&amp;#160;&amp;#160;// Get the current date parameters.&lt;br /&gt;&amp;#160;&amp;#160;var date = new Date();&lt;br /&gt;&amp;#160;&amp;#160;var day = date.getDate();&lt;br /&gt;&amp;#160;&amp;#160;var month = date.getMonth();&lt;br /&gt;&amp;#160;&amp;#160;var year = date.getFullYear();&lt;br /&gt;&amp;#160;&amp;#160;&lt;br /&gt;&amp;#160;&amp;#160;var months = new Array(&#39;January&#39;,&#39;February&#39;,&#39;March&#39;,&#39;April&#39;,&#39;May&#39;,&#39;June&#39;,&#39;July&#39;,&#39;August&#39;,&#39;September&#39;,&#39;October&#39;,&#39;November&#39;,&#39;December&#39;);&lt;br /&gt;&amp;#160;&amp;#160;var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);&lt;br /&gt;&amp;#160;&amp;#160;var weekDay = new Array(&#39;Mo&#39;,&#39;Tu&#39;,&#39;We&#39;,&#39;Th&#39;,&#39;Fr&#39;,&#39;Sa&#39;,&#39;Su&#39;);&lt;br /&gt;&amp;#160;&amp;#160;var days_in_this_month = monthDays[month];&lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;// Create the basic Calendar structure.&lt;br /&gt;&amp;#160;&amp;#160;var calendar_html = &#39;&amp;lt;table class=&quot;calendarTable&quot;&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;tr&amp;gt;&amp;lt;td class=&quot;monthHead&quot; colspan=&quot;7&quot;&amp;gt;&#39; + months[month] + &#39; &#39; + year + &#39;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;var first_week_day = new Date(year, month, 1).getDay();&lt;br /&gt;&amp;#160;&amp;#160;for(week_day= 0; week_day &amp;lt; 7; week_day++) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;td class=&quot;weekDay&quot;&amp;gt;&#39; + weekDay[week_day] + &#39;&amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;}&lt;br /&gt;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;// Fill the first week of the month with the appropriate number of blanks.&lt;br /&gt;&amp;#160;&amp;#160;for(week_day = 0; week_day &amp;lt; first_week_day; week_day++) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;td&amp;gt; &amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;}&lt;br /&gt;&amp;#160;&amp;#160;week_day = first_week_day;&lt;br /&gt;&amp;#160;&amp;#160;for(day_counter = 1; day_counter &amp;lt;= days_in_this_month; day_counter++) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;week_day %= 7;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;if(week_day == 0)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;// Do something different for the current day.&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;if(day == day_counter) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;td class=&quot;currentDay&quot;&amp;gt;&#39; + day_counter + &#39;&amp;lt;/td&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;} else {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;td class=&quot;monthDay&quot;&amp;gt; &#39; + day_counter + &#39; &amp;lt;/td&amp;gt;&#39;;&lt;br /&gt; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;week_day++;&lt;br /&gt;&amp;#160;&amp;#160;}&lt;br /&gt;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;/tr&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;calendar_html += &#39;&amp;lt;/table&amp;gt;&#39;;&lt;br /&gt;&amp;#160;&amp;#160;// Display the calendar.&lt;br /&gt;&amp;#160;&amp;#160;document.write(calendar_html);&lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;h2&amp;gt; Basic Calendar &amp;lt;/h2&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;calendar();&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/06/calendar-using-javascript-with-css.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-aTxkekx83oQ/T-THiD2H0uI/AAAAAAAAADE/HIF5n_bQ24c/s72-c/cal1.JPG" height="72" width="72"/><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-7022903577442146868</guid><pubDate>Tue, 19 Jun 2012 16:46:00 +0000</pubDate><atom:updated>2012-07-24T17:02:45.301+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>Scroll to Top effect using jQuery</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Many of us have been using jQuery lately, but as it goes i was very much contend with using javascript, much because of learning another language. Then i had to use jQuery for one of my company projects, and alas i hoped that i had learned jQuery much earlier.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;jQuery is a simple and tiny javascript framework which provides you with custom functions which may take days to code in native javascript. Here is one of the easy and small example of jQuery that i learned during the project. This will give you an insight of how much simpler jQuery is. You can freely download jQuery from &lt;/span&gt;&lt;a href=&quot;http://jquery.com/&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;In informational sites you might have seen a &#39;Top&#39; link which actually moves you to the top of the web page. This link actually links to the &#39;#top&#39; html anchor which signifies the top of the body. Though helpful it is a bit old and abrupt. How about we replace it with a animated scroll to top button?&lt;br /&gt;&lt;br /&gt;Using the old approach, you can place the below code in your body tag.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;a href=&quot;#top&quot;&amp;gt;&amp;lt;img src=&quot;top.jpg&quot; alt=&quot;back to top&quot;/&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;Clicking&amp;nbsp;on this link will take you to the top of the page. (Make sure the page spans more than the browser window, so that the scrolls are enabled and you can see the result. Also place the link somewhere at the end of the page.)&lt;br /&gt;&lt;br /&gt;You can use this sample image.&amp;nbsp; &lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-FUdgM_-7cZ8/UA0u8N8SXhI/AAAAAAAAAIA/Mq3woSFMpqQ/s1600/top.jpg&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;Now we will do with jQuery with an added scrolling effect. First create a div (somewhere at the end of page) and place the image in this div. Clcking on this image will scroll you to the top of the page.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;div id=&quot;topArrow&quot;&amp;gt; &amp;lt;img src=&quot;top.jpg&quot; alt=&quot;scroll to top&quot; /&amp;gt; &amp;lt;/div&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now the jquery part,&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;$(document).ready(function() {&lt;br /&gt;    // function for scrolling the body to top.&lt;br /&gt;    $(&#39;#topArrow&#39;).click(function () {&lt;br /&gt;&lt;br /&gt;        // animate the page to scroll to top when the link is clicked&lt;br /&gt;        $(&#39;body,html&#39;).animate({&lt;br /&gt;            scrollTop: 0&lt;br /&gt;        }, 1000);&lt;br /&gt;        return false;&lt;br /&gt;    });&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;When the &#39;topArrow&#39; div is clicked, it will animate the body &amp;nbsp;by scrolling it to the top. We use jQuery&#39;s &#39;animate&#39; function for this. The first parameter to animate method denotes the properties or simply the type of animation (we can also have fade, opacity,left etc). The second parameter denotes duration of animation. Other optional parameters include ease and function to call once the animation is complete. Detailed info for &#39;animate&#39; function can be found from &lt;a href=&quot;http://api.jquery.com/animate/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;If you want to the arrow link to be always displayed at the bottom of the page, rather than placing it in the page, the following css enables you to do so.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;/* Style for the arrow link */&lt;br /&gt;#topArrow {&lt;br /&gt; position:fixed;&lt;br /&gt; bottom:0%;&lt;br /&gt; left:75%;&lt;br /&gt; display: block;&lt;br /&gt; padding:20px;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The position:fixed css attribute let the image to stay fixed at the bottom the page.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note&lt;/b&gt;: In some IE versions the position:fixed attribute doesn&#39;t work properly and the link will remain at top of the page. Place the below Doctype declaration on the top of the page code (above html tag), to resolve this problem.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;If you are not into coding or want a simpler solution you can try the &lt;a href=&quot;http://www.mattvarone.com/web-design/uitotop-jquery-plugin/&quot; target=&quot;_blank&quot;&gt;UItoTop jQuery Plugin&lt;/a&gt;. It’s easy to setup with only one line of code, and it works cross-browser nicely.&lt;br /&gt;&lt;br /&gt;You can download the code for this post from &lt;a href=&quot;http://www.4shared.com/zip/BU0NzN0F/jQueryScrollTop_scriptSplash.html&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;Thats all for this post. Will be back with more interesting jQuery snippets.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/06/scroll-to-top-effect-using-jquery.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-FUdgM_-7cZ8/UA0u8N8SXhI/AAAAAAAAAIA/Mq3woSFMpqQ/s72-c/top.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-722060288805359456</guid><pubDate>Fri, 15 Jun 2012 17:34:00 +0000</pubDate><atom:updated>2012-07-27T18:29:35.833+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Auto Refresh in Javascript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-x1cSgRf0eg8/UA00xQollVI/AAAAAAAAAIQ/oCS8gJoHXRo/s1600/refresh.jpeg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-x1cSgRf0eg8/UA00xQollVI/AAAAAAAAAIQ/oCS8gJoHXRo/s1600/refresh.jpeg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;background-color: white;&quot;&gt;There are times when we will need a page to be automatically refreshed. This is mainly seen in pages where the data varies with time and needs to be constantly updated over the timeframe. Eg: Live Score Cards, Live Election results etc.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;There are two ways by which we can perform a HTML Page refresh.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;b&gt;i)&lt;/b&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Using the HTML META tag in the header of the page&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;b&gt;ii)&lt;/b&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Using Javascript - Location.reload&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Using Meta Header Tag&lt;/b&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;This way is useful when the user has disabled javascript on his browser. You need to place the below code inside your webpage’s Head tag.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;meta http-equiv=&quot;refresh&quot; content=&quot;60&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;The value of the content attribute determines the time interval (in seconds) for the page refresh. The above code will automatically refresh the page in 1 minute (60 seconds).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Using Javascript&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;The Location.reload method forces a reload of the window&#39;s current document, i.e. the one contained in the Location.href property. The below javscript function will automatically reload the page after the given time period. In the below function the refresh interval (in milliseconds) is passed as the method argument.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function autoRefresh(refreshPeriod) {&lt;br /&gt;    setTimeout(&quot;window.location.reload();&quot;,refreshPeriod);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;The function then needs to be called from the HTML Body tag’s onLoad event.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;body onload=&quot;autoRefresh(60000);&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;This will cause the page to be refreshed every 1 minute (60000 milliseconds = 60 seconds) after it have been loaded.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;The location.reload method behaves in exactly the same way as the browser&#39;s reload button which, by default reloads from the cache. If, however, the user wants to reload the document from the server (for fetching an updated version), he can do so by supplying &#39;true&#39; as the parameter to the reload method. Thus location.reload(true) will force the page to be loaded from server rather than from the cache.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;Having an automatic page refresh might be quite annoying sometimes, as reloading a content rich page might consume considerable bandwidth for the user, thus resulting in a bad interaction experience with the website. This can be avoided by having a User interactive refresh rather than an auto refresh. This can be done by calling the location.reload method on a Link.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;a href=&quot; window.location.reload();&quot;&amp;gt; Refresh this page &amp;lt;/a&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Or you can do this using a button click. &lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;input type=&quot;button&quot; value=&quot;Reload Page&quot; onClick=&quot;window.location.reload();&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;This will cause the page to be reloaded instantly. The link will cause the page to be reloaded from the cache, while click on the button will cause the page to be reloaded from server.&lt;br /&gt;&lt;br /&gt;There is also another way in javascript to refresh a page using window.location&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;input type=&quot;button&quot; value=&quot;Reload Page&quot; onClick=&quot; window.location = document.URL;&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;The minus with this approach is that, this will not reload the page if there is a hash (#) in the URL.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Meta Refresh vs. Javascript Refresh&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;Meta refresh usage is discouraged by the W3C over other methods. Therefore you should use JavaScript as your primary means for automatic page refreshes and a META tag as your fallback.&lt;br /&gt;Below are the drawbacks of using Meta tag for refresh:&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;If a page redirects too quickly (less than 2-3 seconds), using the &quot;Back&quot; button on the next page may cause some browsers to move back to the redirecting page, whereupon the redirect will occur again. This is bad for usability, as this may cause a reader to be &quot;stuck&quot; on the last website.&lt;/li&gt;&lt;li&gt;A reader may or may not want to be redirected to a different page, which can lead to user dissatisfaction or raise concerns about security&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;You can find the entire code along with html from&amp;nbsp;&lt;b&gt;&lt;a href=&quot;http://www.4shared.com/office/qj4NKeZY/code-00080-Auto-Refresh.html&quot; target=&quot;_blank&quot;&gt;&lt;/a&gt;below.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;noscript&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;meta http-equiv=&quot;refresh&quot; content=&quot;60&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/noscript&amp;gt;&lt;br /&gt;&amp;lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;/** Function to refresh the page at specified interval. **/&lt;br /&gt;&amp;#160;&amp;#160;function autoRefresh(refreshPeriod) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;setTimeout(&quot;window.location.reload();&quot;,refreshPeriod);&lt;br /&gt;&amp;#160;&amp;#160;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body onload=&quot;autoRefresh(10000);&quot;&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;h2&amp;gt; Auto Refresh &amp;lt;/h2&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;This page autorefreshes in 10 seconds. &amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;You can also perform a manual refresh by clicking any of the link/button below.&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;a href=&quot;javascript:window.location.reload();&quot;&amp;gt; Refresh this page - Cached &amp;lt;/a&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;input type=&quot;button&quot; value=&quot;Reload Page - Server&quot; onClick=&quot;window.location.reload(true);&quot;&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;input type=&quot;button&quot; value=&quot;Reload Page&quot; onClick=&quot; window.location = document.URL;&quot;&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/06/auto-refresh-in-javascript.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-x1cSgRf0eg8/UA00xQollVI/AAAAAAAAAIQ/oCS8gJoHXRo/s72-c/refresh.jpeg" height="72" width="72"/><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-2502924215315504589</guid><pubDate>Tue, 29 May 2012 19:16:00 +0000</pubDate><atom:updated>2012-07-24T17:03:16.439+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Password Strength Checker using Javascript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://3.bp.blogspot.com/-Zw0rx82hr18/UA14QBfhtTI/AAAAAAAAAJY/tYbIkYTgpW8/s1600/password-strength.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://3.bp.blogspot.com/-Zw0rx82hr18/UA14QBfhtTI/AAAAAAAAAJY/tYbIkYTgpW8/s1600/password-strength.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;You must have seen in sites like Gmail, when we fill the SignUp or Registration form, the strength of our password is shown as we enter our password. This functionality is very useful in helping us determine our password’s strength and thus securing our account more strongly against any password hacking attacks.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;In this post we shall discuss how to make such a Password Strength Checker using javascript. The following checks are done to assess the strength of the password: &lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If password has more than 6 characters&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If password has both lower and uppercase characters&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If password has a number&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If password has at least one special character&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If password has more than 12 characters&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;A password conforming to all these conditions can be termed as the strongest password. The Password Strength Checker dose not perform any password validations, and only checks the strength of the password.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;The form for the example consists of a password field and a div field which shows the strength of the password.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;form&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Password: &amp;lt;input type=&quot;password&quot; name=&quot;pword&quot; id=&quot;pword&quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div id=&quot;passwordStrength&quot; class=&quot;default&quot; &amp;gt; Enter your password &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;/pre&gt;The javascript method for analyzing the password strength is given below :&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function returnPasswordStrength(password) {&lt;br /&gt;&lt;br /&gt;        var msg = new Array(&quot;Very Weak&quot;, &quot;Weak&quot;, &quot;Better&quot;, &quot;Medium&quot;, &quot;Strong&quot;, &quot;Strongest&quot;);&lt;br /&gt;        var strength   = 0;&lt;br /&gt;&lt;br /&gt;        // If password has more than 6 characters add one to strength&lt;br /&gt;        if (password.length &amp;gt; 6) strength++;&lt;br /&gt;&lt;br /&gt;        //if password has both lower and uppercase characters add one to strength&lt;br /&gt;        if ( ( password.match(/[a-z]/) ) &amp;amp;&amp;amp; ( password.match(/[A-Z]/) ) ) strength++;&lt;br /&gt;&lt;br /&gt;        //if password has at least one numeral add one to strength&lt;br /&gt;        if (password.match(/\d+/)) strength++;&lt;br /&gt;&lt;br /&gt;        //if password has at least one special character add one to strength&lt;br /&gt;        if ( password.match(/.[!,@,#,$,%,^,&amp;amp;,*,?,_,~,-,(,),&#39;\s&#39;]/) ) strength++;&lt;br /&gt;&lt;br /&gt;        //If password has more than 12 characters add one to strength&lt;br /&gt;        if (password.length &amp;gt; 12) strength++;&lt;br /&gt;&lt;br /&gt;        document.getElementById(&quot;passwordStrength&quot;).innerHTML = msg[strength];&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; In the above code we will check whether the entered password satisfies each condition. For each satisfied condition the strength is increased by one. The ‘msg’ array stores the messages for each strength value. The message corresponding to the current strength is shown in the ‘passwordStrength’ div.&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; As the password strength needs to be checked after entering every character, the javascript function for assessing the strength needs to be called on KeyUp event of the password field.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;input type=&quot;password&quot; name=&quot;pword&quot; id=&quot;pword&quot; onkeyup=&quot;returnPasswordStrength(this.value)&quot; /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; The background color of the div can also be changed be defining CSS classes for each strength value and applying them on KeyUp event.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;document.getElementById(&quot;passwordStrength&quot;).className = &quot;class&quot;+strength;&lt;br /&gt;&lt;/pre&gt;You can download the entire code from &lt;a href=&quot;http://www.4shared.com/office/c3UslF8H/code-00070-Password_Strength_C.html&quot;&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;.&lt;br /&gt;Please do enter your valuable comments and suggestions&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2012/02/password-strength-checker-using.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-Zw0rx82hr18/UA14QBfhtTI/AAAAAAAAAJY/tYbIkYTgpW8/s72-c/password-strength.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-5931434795150242187</guid><pubDate>Sun, 06 May 2012 14:52:00 +0000</pubDate><atom:updated>2012-07-27T18:40:48.204+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Simple Ajax Request – Loading a Text File</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Before starting the tutorial just a look into “what is Ajax ?”. Ajax is nor a programming language neither it is a framework, instead it is a technique used to develop Rich Internet Applications, i.e. applications which are more faster, more graphical and more interactive. AJAX (Asynchronous Javascript and XML)  is a combination of several existing technologies rather than being a single technology.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Here are the basic technologies involved in Ajax applications:&lt;br /&gt;• &lt;b&gt;HTML&lt;/b&gt; is used to build Web forms and identify fields for use in the rest of your application. &lt;br /&gt;• &lt;b&gt;JavaScript&lt;/b&gt; code is the core code running Ajax applications and it helps facilitate communication with server applications.&lt;br /&gt;• &lt;b&gt;DHTML&lt;/b&gt;, or Dynamic HTML, is used to update your forms dynamically. You&#39;ll use div, span, and other dynamic HTML elements to mark up your HTML.&lt;br /&gt;• Server Side Scripting is used  to process the query passed by Javascript and returns the response XML.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thus with AJAX, a JavaScript can communicate directly with the server(with a XMLHttpRequest object) and transfer data with it asynchronously, i.e. without submitting or reloading the page. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The XMLHttpRequest object is the key component to Ajax that makes it so useful. It provides a mechanism for the client (through Javascript) to send information to the server and receive a response as XML. The response XML can be processed in the background and used to dynamically update elements on the page. In effect, information flows, and screens are updated, more like in desktop applications than in old-fashioned web applications.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now lets move to the coding part. Basic AJAX is normally composed of three JavaScript functions:&lt;br /&gt;&lt;b&gt;1.&lt;/b&gt; A function that creates an XMLHttpRequest object&lt;br /&gt;&lt;b&gt;2.&lt;/b&gt; A function that calls the XMLHttpRequest object  and submits it, &lt;br /&gt;&lt;b&gt;3.&lt;/b&gt; A function that handles the information sent back to the page. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lets look at the first function. We first declare a variable with name xmlHttp. Now depending upon the browser an ‘xmlHttpRequest’ object is created and assigned to this variable. If the browser is Internet Explorer, then we have to create a new ‘ActiveX Object’ and if the browser is Firefox,Opera etc, then we have to create a new ‘xmlHttpRequest’ which is a built in object. The function then returns the just constructed object.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function getXMLHttp() {&lt;br /&gt;        var xmlhttp;&lt;br /&gt;        if (window.ActiveXObject) {&lt;br /&gt;                xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);&lt;br /&gt;        } else if (window.XMLHttpRequest) {&lt;br /&gt;                xmlhttp = new XMLHttpRequest();&lt;br /&gt;        } else {&lt;br /&gt;                alert(&quot;Your browser does not support Ajax!&quot;);&lt;br /&gt;        }&lt;br /&gt;        return xmlhttp;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In the second function we use the first function to create an xmlHttpRequest object and open an HTTP connection using it. We pass three parameters in the xmlHttp.Open() function. First is the submit method (it can be POST or GET). Second is the URL ,which contains the serve side processing (here to make it simple we are using a text file) and which returns the result to the script as xml. Third is a boolean value which states whether the transfer is asynchronus(true) or synchronus(false). Next we call a function when the ready state changes(loaded, loading, not loaded). Next we send a null to the other page (may not be null for POST , but always null for GET).&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function getTextInfo() {&lt;br /&gt;&amp;nbsp;&amp;nbsp; xmlhttp1=getXMLHttp();&lt;br /&gt;&amp;nbsp;&amp;nbsp; var url =”&quot;demo1.txt” ;&lt;br /&gt;&amp;nbsp;&amp;nbsp; xmlhttp1.open(&quot;GET&quot;,url,true); &lt;br /&gt;&amp;nbsp;&amp;nbsp; xmlhttp1.onreadystatechange = updateInfo;&lt;br /&gt;&amp;nbsp;&amp;nbsp; xmlhttp1.send(null); &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In the ‘readyStateChange’unction we first check whether the reponse is loaded (whether state is 4). The content of the text file is transferred as reponse text. We get the content into a variable and display it to a textarea/div.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function updateInfo() {&lt;br /&gt;        if (xmlhttp1.readyState == 4) {&lt;br /&gt;              var response = xmlhttp1.responseText;&lt;br /&gt;              document.getElementById(&quot;showText&quot;).value = response;&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This sums up the tutorial. Hope this was useful for you.&lt;br /&gt;Please enter your valuable comments and suggestions. &lt;br /&gt;You can find the entire code along with html from below.&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;ScriptSplash.com - Simple Ajax Request&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;script language=&quot;javascript&quot;&amp;gt;&lt;br /&gt;/** Create a cross-browser XMLHttp Request object. **/&lt;br /&gt;function getXMLHttp() {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;var xmlhttp;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;if (window.ActiveXObject) {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;} else if (window.XMLHttpRequest) {&lt;br /&gt;&amp;#160;&amp;#160; &amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp = new XMLHttpRequest();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;} else {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;alert(&quot;Your browser does not support XMLHTTP!&quot;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;return xmlhttp;&lt;br /&gt;}&lt;br /&gt;/** Get the content of a text file using Ajax. **/&lt;br /&gt;function getTextInfo() {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp1=getXMLHttp();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp1.open(&quot;GET&quot;,&quot;AjaxTest.txt&quot;,true); &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp1.onreadystatechange = updateInfo;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;xmlhttp1.send(null); &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;return false;&lt;br /&gt;}&lt;br /&gt;/** Check for response and update the text-area. **/&lt;br /&gt;function updateInfo() { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;if(xmlhttp1.readyState == 4) { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;response=xmlhttp1.responseText;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;document.getElementById(&quot;showText&quot;).value = response; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;} &lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;h2&amp;gt;Get content of text file&amp;lt;/h2&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;h4&amp;gt;Click to get the content of a text file via AJAX&amp;lt;/h4&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;form&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;textarea name=&quot;showText&quot; id=&quot;showText&quot; style=&quot;width:400px;height:400px;&quot;&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;input type=&quot;button&quot; value=&quot;Get Content&quot; onClick=&quot;getTextInfo();&quot;&amp;gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;lt;form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/simple-ajax-request-loading-text-file.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-1308281073238820028</guid><pubDate>Sun, 29 Apr 2012 13:00:00 +0000</pubDate><atom:updated>2012-07-27T17:50:27.700+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>City World Clock - based on Timezone</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-xK8fwdDK_BY/UA1xRl0TB9I/AAAAAAAAAJI/-ZrF6xIdMcI/s1600/city_clock.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-xK8fwdDK_BY/UA1xRl0TB9I/AAAAAAAAAJI/-ZrF6xIdMcI/s1600/city_clock.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;This javascript code snippet allows you to display the time of a city, selected from a drop-down. It also loads the current time and location as the default time and location (based on computer’s timezone and entry in dropdown list). The script is mostly similar to the &lt;a href=&quot;http://scriptden.blogspot.com/2009/07/world-clock-based-on-timezone.html&quot;&gt;previous post&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Here also we have 2 divs for displaying time and date, and a select drop-down for selecting city. We are also using the &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘calculateZoneTime()’&lt;/span&gt;  function for calculating time. Also we have slightly modified &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘returnFormattedDate()’&lt;/span&gt; and &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘returnFormattedTime()’&lt;/span&gt;  functions for formatting date and time respectively. The time is updated every second using the &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘init()’&lt;/span&gt; function. The &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘init()’&lt;/span&gt; function calls the &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘calculateZoneTime()’&lt;/span&gt;  function and repeats itself using the setInterval function.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; An additional function is &lt;span style=&quot;color: #3d85c6;&quot;&gt;‘calculateDefaultTimeZone()’&lt;/span&gt;  which is used for calculating the computer’s current timezone and setting the html select to that particular timezone on page load.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function calculateDefaultTimeZone() {&lt;br /&gt;&lt;br /&gt;      var newDate = new Date();&lt;br /&gt;      // finding current timezone – received in decimal format&lt;br /&gt;      var timeOffset = (newDate.getTimezoneOffset()/60) * (-1);&lt;br /&gt;      var timeZone1 = document.getElementById(&#39;selectTZ1&#39;);&lt;br /&gt;      // converting decimal format to “hh:mm” format&lt;br /&gt;      var formattedOffset = formatTimeOffset(timeOffset);&lt;br /&gt;  &lt;br /&gt;      if (timeZone1)  {&lt;br /&gt;            // finding the current timezone in select drop-down&lt;br /&gt;            for (var i = 0; i &amp;lt; timeZone1.options.length; i++) {&lt;br /&gt;                  if (timeZone1.options[i].value == formattedOffset) {&lt;br /&gt;                        timeZone1.selectedIndex = i;&lt;br /&gt;                         break;&lt;br /&gt;                  }&lt;br /&gt;            }&lt;br /&gt;      }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The ‘formatTimeOffset()’ function converts the calculated current time zone from a decimal format to a&amp;nbsp; “+/-hh:mm”&amp;nbsp; format. Here is the function code: &lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function formatTimeOffset(value) {&lt;br /&gt;      var hours = parseInt(value);&lt;br /&gt;      value -= parseInt(value);&lt;br /&gt;      value *= 60;&lt;br /&gt;      var mins = Math.abs(parseInt(value));&lt;br /&gt;      var display_hours = hours; &lt;br /&gt;      var display_mins = (mins &amp;lt; 10) ? (&quot;0&quot; + mins) : mins;&lt;br /&gt;      timeZoneString = display_hours + &quot;:&quot; + display_mins;&lt;br /&gt;      return timeZoneString;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Please enter your valuable comments and suggestions.&lt;br /&gt;You can find the entire code along with html from below.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt; City clock &amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;style&amp;gt;&lt;br /&gt;  body {&lt;br /&gt;      text-align : center;&lt;br /&gt;  }&lt;br /&gt;  select {&lt;br /&gt;      width: 130px;&lt;br /&gt;  }&lt;br /&gt;  .mainDiv {&lt;br /&gt;      width : 170px;&lt;br /&gt;      height : 170px;&lt;br /&gt;      background-color : #eeeeee; &lt;br /&gt;  }&lt;br /&gt;  .subDiv {&lt;br /&gt;      width : 130px;&lt;br /&gt;  }&lt;br /&gt;  #timeText1 {&lt;br /&gt;      font-size: 17px;&lt;br /&gt;      font-weight: bold;&lt;br /&gt;      font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;&lt;br /&gt;  }&lt;br /&gt;  #dayText1 {&lt;br /&gt;      font-size: 13px;&lt;br /&gt;      font-weight: bold;&lt;br /&gt;      font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;&lt;br /&gt;  }&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&amp;lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&amp;gt;&lt;br /&gt;/** Initialises the Clock and updates it every second. **/&lt;br /&gt;function init() {&lt;br /&gt;    try {&lt;br /&gt;        clearInterval(myInterval);&lt;br /&gt;    } catch(err){}&lt;br /&gt;    calculateZoneTime(document.getElementById(&quot;selectTZ1&quot;).value,1);&lt;br /&gt;    myInterval = setInterval(&quot;init()&quot;,1000);&lt;br /&gt;}&lt;br /&gt;/** Calculates the default time zone based on System Date. **/&lt;br /&gt;function calculateDefaultTimeZone() {&lt;br /&gt;    var newDate = new Date();&lt;br /&gt;    var timeOffset = (newDate.getTimezoneOffset()/60) * (-1);&lt;br /&gt;    var timeZone1 = document.getElementById(&#39;selectTZ1&#39;);&lt;br /&gt;    var formattedOffset = formatTimeOffset(timeOffset);&lt;br /&gt;    if (timeZone1) {&lt;br /&gt;        for (var i = 0; i &amp;lt; timeZone1.options.length; i++) {&lt;br /&gt;            if (timeZone1.options[i].value == formattedOffset) {&lt;br /&gt;                timeZone1.selectedIndex = i;&lt;br /&gt;                break;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;/** Format the time based upon the time-zone. **/&lt;br /&gt;function formatTimeOffset(value) {&lt;br /&gt;    var hours = parseInt(value);&lt;br /&gt;    var mins = Math.abs((value - parseInt(value)) * 60);&lt;br /&gt;    var display_mins = (mins &amp;lt; 10) ? (&quot;0&quot; + mins) : mins;&lt;br /&gt;    var timeZoneString = hours + &quot;:&quot; + display_mins;&lt;br /&gt;    return timeZoneString;&lt;br /&gt;}&lt;br /&gt;/** Populates the Time and Date based upon the current time-zone. **/&lt;br /&gt;function calculateZoneTime(TimeZoneOffset,num) {&lt;br /&gt;    var lDate = new Date();&lt;br /&gt;    try {&lt;br /&gt;        var timeArray = TimeZoneOffset.split(&quot;:&quot;);&lt;br /&gt;        var hrs = parseInt(timeArray[0]);&lt;br /&gt;        var mins = parseInt(timeArray[1])/60;&lt;br /&gt;    } catch(err){}&lt;br /&gt;    var ZoneOffset = (hrs &amp;gt; 0) ? (hrs + mins) : (hrs - mins);&lt;br /&gt;    var ms = lDate.getTime() + (lDate.getTimezoneOffset() * 60000) + ZoneOffset * 3600000;&lt;br /&gt;    var time =new Date(ms);&lt;br /&gt;    var dayText = returnFormattedDate(time);&lt;br /&gt;    var timeText =returnFormattedTime(time);&lt;br /&gt;    document.getElementById(&quot;timeText&quot;+num).innerHTML=timeText;&lt;br /&gt;    document.getElementById(&quot;dayText&quot;+num).innerHTML=dayText;&lt;br /&gt;}&lt;br /&gt;/** Returns the Time in HH:mm:ss PM/AM format. **/&lt;br /&gt;function returnFormattedTime(dateObject) {&lt;br /&gt;    var hrs = dateObject.getHours();&lt;br /&gt;    var mins = dateObject.getMinutes();&lt;br /&gt;    var seconds = dateObject.getSeconds();&lt;br /&gt;    var meridian = &quot;am&quot;;&lt;br /&gt;    if (hrs == 12) {&lt;br /&gt;        meridian = &quot;pm&quot;;&lt;br /&gt;    } else if(hrs == 0) {&lt;br /&gt;        meridian = &quot;am&quot;;&lt;br /&gt;        hrs = 12;&lt;br /&gt;    } else if(hrs &amp;gt; 12) {&lt;br /&gt;        meridian = &quot;pm&quot;;&lt;br /&gt;        hrs = hrs - 12;&lt;br /&gt;    }&lt;br /&gt;    display_hours = (hrs &amp;lt; 10) ? (&quot;0&quot; + hrs) : hrs;&lt;br /&gt;    display_mins = (mins &amp;lt; 10) ? (&quot;0&quot; + mins) : mins;&lt;br /&gt;    display_seconds = (seconds &amp;lt; 10) ? (&quot;0&quot; + seconds) : seconds;&lt;br /&gt;    return ( display_hours + &quot; : &quot; + display_mins +&quot; : &quot; + display_seconds +&quot;&quot; + meridian);&lt;br /&gt;}&lt;br /&gt;/** Returns the Date in Day,dd:MMM:yyyy format **/&lt;br /&gt;function returnFormattedDate(dateObject) {&lt;br /&gt;    var tdate = dateObject.getDate();&lt;br /&gt;    var tmnth = dateObject.getMonth()&lt;br /&gt;    var tyear = dateObject.getYear() ;&lt;br /&gt;    var tday= dateObject.getDay();&lt;br /&gt;    var mnthName = new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;);&lt;br /&gt;    var dayName = new Array(&quot;sun&quot;,&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thu&quot;,&quot;Fri&quot;,&quot;Sat&quot;);&lt;br /&gt;    return ( dayName[tday] + &quot; , &quot; + tdate + &quot; &quot; + mnthName[tmnth] + &quot; &quot; + tyear ); &lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt; &lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body onload=&quot;calculateDefaultTimeZone();init();&quot;&amp;gt;&lt;br /&gt;&amp;lt;div align=&quot;center&quot; class=&quot;mainDiv&quot;&amp;gt;&lt;br /&gt;&amp;lt;h3&amp;gt;&amp;lt;u&amp;gt;&amp;lt;i&amp;gt; CITY CLOCK &amp;lt;/i&amp;gt;&amp;lt;/u&amp;gt;&amp;lt;/h3&amp;gt;&lt;br /&gt;&amp;lt;div id=&quot;div1&quot; class=&quot;subdiv&quot;&amp;gt;&lt;br /&gt;    &amp;lt;div name=&quot;timeText1&quot; id=&quot;timeText1&quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;    &amp;lt;div name=&quot;dayText1&quot; id=&quot;dayText1&quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;    &amp;lt;select name=&#39;selectTZ1&#39; id=&#39;selectTZ1&#39;&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;4:00&#39;&amp;gt;Abu Dhabi&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;9:30&#39;&amp;gt;Adelaide&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Amsterdam&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Athens&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Baghdad&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;7:00&#39;&amp;gt;Bangkok&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Beijing&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Berlin&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-3:00&#39;&amp;gt;Brasilia&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-3:00&#39;&amp;gt;Beunos Aires&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:30&#39;&amp;gt;Bangalore&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Budapest&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Brussels&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Cairo&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Cape Town&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;10:00&#39;&amp;gt;Canberra&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-6:00&#39;&amp;gt;Chicago&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Copenhagen&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:30&#39;&amp;gt;Colombo&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:30&#39;&amp;gt;Chennai&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Doha&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-6:00&#39;&amp;gt;Dallas&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Dar es Salaam&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;6:00&#39;&amp;gt;Dhaka&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;4:00&#39;&amp;gt;Dubai&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;0:00&#39;&amp;gt;Greenwich&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Harare&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-5:00&#39;&amp;gt;Havana&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Helsinki&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;HongKong&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:00&#39;&amp;gt;Islamabad&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Istanbul&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;7:00&#39;&amp;gt;Jakarta&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;2:00&#39;&amp;gt;Jerusalem&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;4:30&#39;&amp;gt;Kabul&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:45&#39;&amp;gt;Kathmandu&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Kuala lampur&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:00&#39;&amp;gt;Karachi&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Lagos&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;0:00&#39;&amp;gt;London&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-8:00&#39;&amp;gt;Los Angeles&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:30&#39;&amp;gt;Mumbai&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Madrid&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Manila&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-6:00&#39;&amp;gt;Mexico city&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Moscow&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;4:00&#39;&amp;gt;Muscat&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Nairobi&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;5:30&#39;&amp;gt;New Delhi&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-5:00&#39;&amp;gt;New York&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Oslo&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Paris&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Perth&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-7:00&#39;&amp;gt;Phoenix&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;9:00&#39;&amp;gt;Pyongyang&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-3:00&#39;&amp;gt;Rio de Janeiro&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:00&#39;&amp;gt;Riyadh&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Rome&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;9:00&#39;&amp;gt;Seoul&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Singapore&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Stockholm&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;10:00&#39;&amp;gt;Sydney&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;8:00&#39;&amp;gt;Taipei&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;3:30&#39;&amp;gt;Tehran&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;9:00&#39;&amp;gt;Tokyo&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-5:00&#39;&amp;gt;Toronto&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Vienna&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;1:00&#39;&amp;gt;Vatican city&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;-5:00&#39;&amp;gt;Washington DC&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;12:00&#39;&amp;gt;Wellington&amp;lt;/option&amp;gt;&lt;br /&gt;        &amp;lt;option value=&#39;6:30&#39;&amp;gt;Yangon&amp;lt;/option&amp;gt;&lt;br /&gt;    &amp;lt;/select&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/city-world-clock-based-on-timezone.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-xK8fwdDK_BY/UA1xRl0TB9I/AAAAAAAAAJI/-ZrF6xIdMcI/s72-c/city_clock.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-4668093846158945775</guid><pubDate>Sun, 22 Apr 2012 13:42:00 +0000</pubDate><atom:updated>2012-07-24T17:03:48.116+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>World Clock  - based on Timezone</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-QhT8c2C69IQ/UA1wDIwZ2hI/AAAAAAAAAJA/TGC4LHaCb28/s1600/world+clock.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-QhT8c2C69IQ/UA1wDIwZ2hI/AAAAAAAAAJA/TGC4LHaCb28/s1600/world+clock.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;This javascript code allows you to display the time based on the user’s timezone. The script makes use of the inbuilt javascript ‘Date’ object and its functions. The clock displays time in the default 12 hour format, that can be changed to a 24 hours using a radio button. Also we are displaying the current day and date. The user can also change the timezone using a select box, and thus can view the current time in different timezones. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First lets have a look at the form. The form consists of two div elements, one for displaying time and the other for displaying day and date. There is a select element (selectTZ) which consists of all time-zones.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;div name=&quot;timeText&quot; id=&quot;timeText&quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div name=&quot;dayText&quot; id=&quot;dayText&quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;select name=&#39;selectTZ&#39; id=&#39;selectTZ&#39;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;option value=&#39;0&#39;&amp;gt;Select your Timezone&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;option value=&#39;-12:00&#39;&amp;gt;(-12:00) International Date Line West&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;option value=&#39;-11:00&#39;&amp;gt;(-11:00) Midway Island, Samoa&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;……………….&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now lets start with the javascript code. First we have to retrieve the current date and time. The following line of code do this.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;// create new date object&lt;br /&gt;  var time =  new Date();&lt;br /&gt;  // get the formatted date value &lt;br /&gt;  var dayText = returnFormattedDate(time);&lt;br /&gt;  // get the formatted time value (hh:mm:ss or hh:mm:ss pm/am))&lt;br /&gt;  var timeText =  returnFormattedTime(time);&lt;br /&gt;  // passing formatted value to divs&lt;br /&gt;  document.getElementById(&quot;timeText&quot;).innerText=timeText;     &lt;br /&gt;  document.getElementById(&quot;dayText&quot;).innerText=dayText;&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The &lt;span style=&quot;color: #351c75;&quot;&gt;‘returnFormattedDate()’&lt;/span&gt; and &lt;span style=&quot;color: #351c75;&quot;&gt;‘returnFormattedTime()’&lt;/span&gt;  functions return the date and time respectively in a specific format. &lt;span style=&quot;color: #351c75;&quot;&gt;‘returnFormattedDate()’&lt;/span&gt; returns date in “Monday, 01 January 2010” format. Whereas, &lt;span style=&quot;color: #351c75;&quot;&gt;‘returnFormattedTime()’&lt;/span&gt;  returns time in any of the two formats, “23:45:06” or “11:45:06 pm”.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The below function retrieves the current date and time based upon the selected time zone and places the formatted date and time on to the respective div elements.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function calculateZoneTime(SelectTimeZone) {&lt;br /&gt;    // if timezone not selected&lt;br /&gt;    if(SelectTimeZone==&#39;0&#39;) {&lt;br /&gt;        document.getElementById(&quot;timeText&quot;).innerText=&quot;00 : 00 : 00&quot;;&lt;br /&gt;        document.getElementById(&quot;dayText&quot;).innerText= &quot;please select a timezone&quot;;&lt;br /&gt;        return true; &lt;br /&gt;    }&lt;br /&gt;    // get current date object&lt;br /&gt;    var zDate = new Date();&lt;br /&gt;    //  retrieve offset of the selected timezone in decimal format &lt;br /&gt;    var ZoneOffset = retrieveOffset(SelectTimeZone);&lt;br /&gt;    // adding the time difference to current time (in milliseconds)&lt;br /&gt;    var ms = zDate.getTime() + (zDate.getTimezoneOffset() * 60000) + ZoneOffset * 3600000;&lt;br /&gt;    // creating a date object using the calculated zone time&lt;br /&gt;    var time =  new Date(ms);&lt;br /&gt;    var dayText = returnFormattedDate(time);&lt;br /&gt;    var timeText =  returnFormattedTime(time);&lt;br /&gt;    document.getElementById(&quot;timeText&quot;).innerText=timeText;&lt;br /&gt;    document.getElementById(&quot;dayText&quot;).innerText=dayText;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; Here the function parameter ‘SelectTimeZone’ is the value of the time-zone that is selected in the ‘selectTZ’ select. The ‘retieveOffset()’ function converts the parameter value from the “4:30” format to a “4.5” format, so that it can be used for date calculations. This offset is then added to the current time, to get the time-zone specific time.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The code for ‘retieveOffset()’ is as follows:&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function retrieveOffset(TimeZoneOffset) {&lt;br /&gt;      try {&lt;br /&gt;         // calculating the whole and decimal part of the offset &lt;br /&gt;         var timeArray = TimeZoneOffset.split(&quot;:&quot;);&lt;br /&gt;         var hrs = parseInt(timeArray[0]);&lt;br /&gt;         var mins = parseInt(timeArray[1])/60; &lt;br /&gt;      } catch(err){&lt;br /&gt;         alert(&quot;error&quot;); &lt;br /&gt;      }&lt;br /&gt;      // adding or deleting the decimal part and whole part&lt;br /&gt;      var offset = (hrs &amp;gt; 0) ? (hrs + mins) : (hrs - mins);&lt;br /&gt;      return offset;  &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now we have to update the time every second. For this we have a ‘init()’ function. The function calls the ‘calculateZoneTime()’  function and repeats itself using the setInterval function.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function init() {&lt;br /&gt;     try {&lt;br /&gt;             // try to clear any interval previously set&lt;br /&gt;              clearInterval(myInterval);&lt;br /&gt;     } catch(err){}&lt;br /&gt;     // calculate time based on select value&lt;br /&gt;     calculateZoneTime(document.getElementById(&quot;selectTZ&quot;).value) ;&lt;br /&gt;     // repeat the init function each sec (1000ms = 1 sec)&lt;br /&gt;     myInterval = setInterval(&quot;init()&quot;,1000);    &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This sums up the article. Hope this helps. This post is open for your queries and suggestions.&lt;br /&gt;You can find the code (with HTML) in its entirety &lt;a href=&quot;http://www.4shared.com/office/FqN9pn3c/code-00040-World_Clock.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. Also you can find an advanced version of the script that loads the current time zone as default on the clock, &lt;a href=&quot;http://www.4shared.com/office/gvZWqaeZ/code-00041-World_Clock_-_Advan.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;.&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/world-clock-based-on-timezone.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-QhT8c2C69IQ/UA1wDIwZ2hI/AAAAAAAAAJA/TGC4LHaCb28/s72-c/world+clock.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-2644577582320319215</guid><pubDate>Sun, 08 Apr 2012 14:47:00 +0000</pubDate><atom:updated>2012-07-27T17:13:56.088+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Set as Homepage, Bookmark a page</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;The following javascript code snippet allows a user to add your page as homepage, and to bookmark (favorite) your page.&lt;br /&gt;&lt;br /&gt;The following javascript function code allows you to add the current address location as the Homepage of your browser.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function setHome(){&lt;br /&gt;      document.body.style.behavior=&#39;url(#default#homepage)&#39;;&lt;br /&gt;      document.body.setHomePage(window.location.href);&lt;br /&gt;}&lt;/pre&gt;The following javascript function code adds the current address location to your browser’s Bookmarks list, and the document title is set as the title (name) of the Bookmark.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function setBookmark() {&lt;br /&gt;      var title = document.title;&lt;br /&gt;      var address =  window.location.href;&lt;br /&gt;      if(window.sidebar) {&lt;br /&gt;            window.sidebar.addPanel(title,address);&lt;br /&gt;      } else if(window.external) {&lt;br /&gt;            window.external.AddFavorite(address,title);&lt;br /&gt;      } else if(window.opera &amp;amp;&amp;amp; window.print) { &lt;br /&gt;            var elem = document.createElement(&#39;a&#39;);&lt;br /&gt;            elem.setAttribute(&#39;href&#39;,address);&lt;br /&gt;            elem.setAttribute(&#39;title&#39;,title);&lt;br /&gt;            elem.setAttribute(&#39;rel&#39;,&#39;sidebar&#39;);&lt;br /&gt;            elem.click();&lt;br /&gt;      }&lt;br /&gt;}&lt;/pre&gt;The problem with the above codes is that the ‘Homepage’ code works only in Internet Explorer, and the ‘Bookmark’ code is somewhat erry in Firefox (bookmarked page opens in sidebar). &lt;br /&gt;But there is another option available though not javascript. The &lt;a href=&quot;http://www.addthis.com/&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;addthis&lt;/b&gt;&lt;/a&gt; site provides you with a custom button that can be added to your site/blog, that not only allows to bookmark your page but also provides you with data related to how and where the content is being shared. &lt;br /&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/set-page-as-your-homepage-bookmark-page.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>7</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-4852042229170717899</guid><pubDate>Fri, 30 Mar 2012 13:56:00 +0000</pubDate><atom:updated>2012-08-07T20:30:35.962+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Show div once a day using Javascript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The following javascript snippet allows you to show a message to the user who visits the page for the first time, and hides the message if he makes subsequent visits to that page on that day. The script involves cookies and thus need to have cookie-enabled in the user browser.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; First lets have a look at the form. The form consists of a single ‘div’ element. The message to be displayed has to be kept inside this div. It can be text or image or any other display content.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;form name=&quot;scriptDen&quot;&amp;gt;&lt;br /&gt;&amp;lt;div id=&quot;myDiv&quot;&amp;gt; Place Your Content Here &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next we have to build the code for creating a cookie. This cookie has to be created when the user visits the page for the first time. The &lt;span style=&quot;color: #0b5394;&quot;&gt;createCookie()&lt;/span&gt; function takes name of the cookie, its value and its expiry period(in days) as parameters. Since we are creating a cookie for a day, the expiry period will be one.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function createCookie(name,value,days) {&lt;br /&gt;    // new Date object created&lt;br /&gt;    var date = new Date();&lt;br /&gt;    // adding days to current time&lt;br /&gt;    date.setTime(date.getTime()+(days*24*60*60*1000));&lt;br /&gt;    // converting to standard format&lt;br /&gt;    var expires = date.toGMTString(); &lt;br /&gt;    // adding cookie name,value and expiry time&lt;br /&gt;    document.cookie = name+&quot;=&quot;+value+&quot;; expires=&quot;+expires+&quot;; path=/&quot;;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now we need to read the cookies stored in the browser and look whether our cookie is present there or not. The &lt;span style=&quot;color: #0b5394;&quot;&gt;readCookie()&lt;/span&gt; function returns true if the cookie stored by us is present and returns false if not.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function readCookie(name) {&lt;br /&gt;    var flag = 0;&lt;br /&gt;    // getting the browser cookie values into an array&lt;br /&gt;    var dcmntCookie = document.cookie.split(&#39;;&#39;);&lt;br /&gt;    // looping over the array to find our cookie&lt;br /&gt;    for(var i=0;i &amp;lt; dcmntCookie.length;i++) {&lt;br /&gt;        var ck = dcmntCookie[i];&lt;br /&gt;        // loop for removing extra spaces from the beginning of each cookie&lt;br /&gt;        while (ck.charAt(0)==&#39; &#39;) {&lt;br /&gt;            ck = ck.substring(1,ck.length);&lt;br /&gt;        }&lt;br /&gt;        if(ck) {&lt;br /&gt;            // splitting the cookie into its name and value&lt;br /&gt;            cparts = ck.split(&#39;=&#39;);&lt;br /&gt;            // setting the flag if a cookie with the name specified exists&lt;br /&gt;            if (cparts[0]==name)&lt;br /&gt;               flag=1;&lt;br /&gt;        }              &lt;br /&gt;    }    &lt;br /&gt;    // returning true if cookie exists else returning false &lt;br /&gt;    if(flag) { &lt;br /&gt;        return true; &lt;br /&gt;    } else {&lt;br /&gt;        return false; &lt;br /&gt;    }   &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now as we have created both the createCookie() and readCookie() function, we have to use them for serving our purpose, that is, to hide/display a div element. This is achieved using the &lt;span style=&quot;color: #0b5394;&quot;&gt;checkCookie()&lt;/span&gt; function. It checks for the cookie with a given name specified. If the cookie exists then it hides the div. Else if the cookie is not present, a new cookie is created and added to the browser. &lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function checkCookie(name) {&lt;br /&gt;    if (readCookie(name)) {&lt;br /&gt;        // hide the div element using css style attribute&lt;br /&gt;        document.getElementById(&#39;myDiv&#39;).style.display = &quot;none&quot;;&lt;br /&gt;        document.getElementById(&#39;myDiv&#39;).style.visibility = &quot;hidden&quot;;&lt;br /&gt;    }&lt;br /&gt;    // create a new cookie if cookie already not present&lt;br /&gt;    else createCookie(name,&quot;cookie 4 the day&quot;,1); &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This sums up the article. Hope this was helpful. Please do enter your valuable comments and suggestions. You can find the entire code along with html from below. &lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;show Msg once a day&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;style&amp;gt;&lt;br /&gt;#myDiv {&lt;br /&gt;    border: 1px solid #999;&lt;br /&gt;    width: 350 px;&lt;br /&gt;    height: 100px;&lt;br /&gt;    background: #ccc;&lt;br /&gt;    text-align: center;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&amp;lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;/** Create a html cookie and set expiry as a day. **/&lt;br /&gt;function createCookie(name,value,days) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; var date = new Date();&lt;br /&gt;&amp;nbsp;&amp;nbsp; date.setTime(date.getTime()+(days*24*60*60*1000));&lt;br /&gt;&amp;nbsp;&amp;nbsp; var expires = date.toGMTString();&lt;br /&gt;&amp;nbsp;&amp;nbsp; document.cookie = name+&quot;=&quot;+value+&quot;; expires=&quot;+expires+&quot;; path=/&quot;;&lt;br /&gt;}&lt;br /&gt;/** Check if already a cookie has been created. **/&lt;br /&gt;function readCookie(name) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var flag = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var dcmntCookie = document.cookie.split(&#39;;&#39;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(var i=0;i &amp;lt; dcmntCookie.length;i++) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var ck = dcmntCookie[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while (ck.charAt(0)==&#39; &#39;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ck = ck.substring(1,ck.length);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(ck) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cparts = ck.split(&#39;=&#39;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cparts[0] == name) flag=1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(flag) { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} else {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;br /&gt;}&lt;br /&gt;/** Check if cookie exists else create a new one. **/&lt;br /&gt;function checkCookie(name) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (readCookie(name)) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;document.getElementById(&#39;myDiv&#39;).style.display = &quot;none&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;document.getElementById(&#39;myDiv&#39;).style.visibility = &quot;hidden&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else createCookie(name,&quot;cookie 4 the day&quot;,1); &lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body onLoad=&quot;checkCookie(&#39;MyCookie&#39;)&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;form name=&quot;scriptDen&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;center&amp;gt;&amp;lt;div id=&quot;myDiv&quot;&amp;gt; Place Your Content Here &amp;lt;/div&amp;gt;&amp;lt;/center&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/show-div-once-day-using-javascript.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2182913583834088147.post-2769310006047972025</guid><pubDate>Sun, 11 Mar 2012 14:55:00 +0000</pubDate><atom:updated>2012-07-24T15:00:39.638+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java Script</category><title>Passing only non-Empty fields on form submit</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Sometimes you have a lot of text-fields and want only those fields to be sent to the server as parameters, which are not left blank (that is which are filled). This helps in decreasing the amount of data being sent to the server.&lt;br /&gt;&lt;br /&gt;This is one way of how the above can be done. Lets first start with the basic HTML. The form (say testform) may consists of a number of text fields and a submit button. When the button named “Submit” is clicked, the javascript function “submitFunc()” is called.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;form name=&quot;testform&quot;&amp;gt;&lt;br /&gt;      &amp;lt;input type=&quot;text&quot; name=&quot;text1&quot; id=&quot;text1&quot;&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;      &amp;lt;input type=&quot;text&quot; name=&quot;text2&quot; id=&quot;text2&quot;&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;      &amp;lt;input type=&quot;text&quot; name=&quot;text3&quot; id=&quot;text3&quot;&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;      &amp;lt;input type=&quot;button&quot; onclick=&quot;submitFunc();&quot; value=&quot;Submit&quot;&amp;gt;&lt;br /&gt;&amp;lt;form&amp;gt;&lt;/pre&gt;Now about the “submitFunc()” function. This function checks each text field by calling the “loopRemove ()” function.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function submitFunc() {&lt;br /&gt;&lt;br /&gt;      loopRemove(&quot;text&quot;,3);&lt;br /&gt;      document.testform.action = &quot;http://scriptden.blogspot.com&quot;;&lt;br /&gt;      document.testform.submit();&lt;br /&gt;}&lt;/pre&gt;The  “loopRemove()” function loops over the fields, who have a similar name with two parts, one a string (say, ‘text’), suffixed by an integer part in increasing order starting with one (eg. ‘txtField1’,’txtField2’,…).It checks whether the given element’s field value is empty, and if found empty it removes the corresponding element by accessing the element’s parent node.&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;function loopRemove(startName,count) {&lt;br /&gt;      for(var i=1;i&amp;lt;=count;i++) {&lt;br /&gt;            if(document.getElementById(startName+i).value==&quot;&quot;) { &lt;br /&gt;                  var t = document.getElementById(startName+i);&lt;br /&gt;                  t.parentNode.removeChild(t); &lt;br /&gt;            }&lt;br /&gt;      } &lt;br /&gt;}&lt;/pre&gt;The action path of the form is specified dynamically and the form is submit using the “form.submit()” function. The form thus submitted (with empty elements removed) will pass only the filled field’s values to the page/location specified in the action path.&lt;a href=&quot;javascript:void(0)&quot;&gt; &lt;/a&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;document.testform.action = &quot;http://scriptden.blogspot.com&quot;;&lt;br /&gt;document.testform.submit();&lt;/pre&gt;This sums up the article. Hope this helps.&lt;br /&gt;This post is open for your queries and suggestions.&lt;br /&gt;&lt;br /&gt;You can download the entire script &lt;a href=&quot;http://www.4shared.com/office/tlwm64Q9/code-00010-Pass_non-empty_para.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;</description><link>http://scriptden.blogspot.com/2009/07/passing-only-non-empty-fields-on-form.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item></channel></rss>