Posts

Showing posts from November, 2011

Javascript Basics : Part 2 (Functions)

Image
Suppose I want to change the button color when it is clicked. And to do this task, I don’t want to send the page to the server. So, what should I do? Well, HTML DOM has given us some events to work with. We will use one the events, Click event and put some code on its event. The below code will change the button color if clicked on that. Now, if we want to add another button and apply same functionality with the click, the code will like below But if you look closely, we are repeating the code. This is the worst smell of the code smells . So, we should create a method and call that method from the click event. So, how do we create a function in Javascript? Its easy. As we did before, we must add the ‘script’ tag first. Inside of this tag, we will write the function. In javascript, we must write the word ‘function’ before the name of the function. For example: function MyFunctionName() { } Then we will call that function from the event. So, if we refac

Javascript Basics : Part 1 (Hello world)

Image
Now a days, the most basic thing a web application developer must know is Javascript. So, what is javascript? Yes..you probably know Javascript is nothing to do with Java. Javascript is totally different with Java. In syntax, in semantic, in usage, in all aspects. So, Javascript is a scripting language which runs in browser. The browser software executes the script line by line, from top to bottom, and display you the output. Now what you can guess, Javascript must be embedded in a HTML file and when the HTML codes are rendered/executed, Javascript codes get executed with those. If you want to let your browser know that you are writing a Javascript code, you must add some extra tags/codes with the statements. For example: What you can see, at the above code, by the ‘document.write’ method, we are writing some text in the browser. But we need to add the 'script' tag having the type as javascript with it. If the browser executes this line of code, we can view ‘ Javascri