Previous
Object Oriented JavaScipt Programming 
Next
String Manipulation 
ABC of JavaScript : An Interactive JavaScript Tutorial
Getting Information

Getting Information

Now let us see how to get information about different things - like find the time. Or to know what browser the user is using. Or his/her resolution. Or to find the answer to life, the universe and everything.

Lets see how to find the time.

var today = new Date()
alert("Hour : "+today.getHours() + "\nMinutes : "+today.getMinutes()+
"\nDay : "+today.getDate()+"\nMonth : "+today.getMonth()+
"\nYear : "+today.getYear())

This will show the current hour, minute, date, month and year. Notice that the month is wrong. That is because in JavaScript, months starts at 0 - so January is 0, February is 1 and so on. Just add 1 to today.getMonth() to correct this.

Now lets see how to find the browser, resolution and other such personal stuff.

The most easiest way to find the browser is
alert("Browser : "+navigator.appName)

But in most cases this is not enough. You have to use other elements like 'navigator.userAgent' to find the correct browser and its version. 'navigator.userAgent' also has information about your operating system which make it extremely useful. One method to do this is...

var str = navigator.userAgent.toString()
array = str.split("; ")
var OS = array[2]
alert("Operating System : "+OS)

Finding Resolution is easy - 'screen.width' gives the width while 'screen.height' gives the screen height.

Want to find the answer to life, the universe and everything? Search for it in Google - the answer is 42.

Try out what you have learned. Please remember that diffrent browsers may give different values for some variables.

Previous
Object Oriented JavaScipt Programming 
Next
String Manipulation 
blog comments powered by Disqus