Online JavaScript Manual - Variables and JavaScript methods
Sponsored Links
Till now we've looked at two important methods, the alert() method of the window object and write() of document object. You've also seen how text or numbers can be passed to these methods.
Let's consider a situation where you plan to display the name of the visitor on the page. To do this you would first have to get the visitor's name (we'll shortly see how to achieve this), store it in a variable and send the result to the document.write() method. Here we'll examine how we can pass variables to JavaScript methods.
document.write("Your name is " + vis_name);
We've assumed that the visitor's name is stored in a variable called vis_name. Note how the variable is written outside the quotes of the write() method. If this variable was included in the quotes, JavaScript would have written vis_name instead of the name of the visitor. Similarly, for the alert() method, we would pass the value as:
alert("Welcome " + vis_name + "\nHow are we today?");
Remember, a variable should be placed outside the quotes to display its value.
- What do you think will be written on the window by the following code?
var url = "http://www.webdevelopersnotes.com/"; document.write("Welcome to <B>" + url + "</B>");
- What will be displayed on the alert box?
var my_movie = "Godfather"; alert("My favorite movie is my_movie");
- What is written on the document?
var sum_total = 6000; document.write("The total is " + "sum_total"); (Note: sum_total contains numeric data)
- Write a function that takes your name as argument and displays in an alert box, a personalized welcome message, something like Welcome, Johnny Bravo.
- Change this function so that it displays the welcome message in the status bar instead of an alert box.
(Note: Use window.defaultStatus property of the window object)
Comments, questions, feedback... whatever!
Page contents:
Recent Articles
Recent Blog Posts
Popular Articles
- Hotmail Sign In page
- Create a Hotmail account - Instructions
- Create Gmail address
- Download and install Outlook Express
- Get your free Gmail address
- Outlook Express new version
- Create my own email address
- Browers for Windows list
- Get email address
- Color combinations for web sites and pages
- Create Yahoo ID
