Using variables with javascript methods - online javascript manual
using variables with javascript methods, online javascript manual
Online JavaScript Manual - Variables and JavaScript methodsGo to Online JavaScript Manual - Variables and JavaScript methodsJavaScript tutorialGo to JavaScript tutorialWeb development tutorialsGo to web development tutorialsHomepage

Main navigation links of JavaScript Tutorial section

Online JavaScript Manual - Variables and JavaScript methods

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.



Assignments
  1. 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>");
    
  2. What will be displayed on the alert box?
       var my_movie = "Godfather";
    
       alert("My favorite movie is my_movie");
       
  3. What is written on the document?
       var sum_total = 6000;
    
       document.write("The total is " + "sum_total");
    
       (Note: sum_total contains numeric data)
    
  4. Write a function that takes your name as argument and displays in an alert box, a personalized welcome message, something like Welcome, Johnny Bravo.
  5. 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)
   Click here for possible answers


Online Workspace

Back Next


AddThis Social Bookmark Button
Page contents: Using variables with javascript methods - online javascript manual

Page URL: http://www.webdevelopersnotes.com/tutorials/javascript/ variables_methods_javascript_manual.php3



Join Mailing List


Feedback/Questions




50+ web hosting FAQs

Search engine for your website

Free software from Google - The Google Pack Collection

Create your own search engine
Search WebDevelopersNotes.com