Writing javascript with html - introduction to javascript - javascript on web pages
writing javascript with html, introduction to javascript, javascript on web pages
JavaScript Introduction - Writing JavaScript with HTMLGo to JavaScript Introduction - Writing JavaScript with HTMLJavaScript tutorialGo to JavaScript tutorialWeb development tutorialsGo to web development tutorialsHomepage

Main navigation links of JavaScript Tutorial section

JavaScript Introduction - Writing JavaScript with HTML

Take a look at the following code:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

document.write("JavaScript is <B>great</B>.");

//-->
</SCRIPT>

You'll notice that along with the text I've thrown in the HTML bold tag. HTML tags enclosed in write() are not displayed. The browser interprets these tags accordingly and then prints the result. In our case, the word 'great' is displayed in bold.
Click here to view the result.

Here is a code that prints text in blue color:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

document.write('<FONT COLOR="#0000FF">Blue denim</FONT>');

//-->
</SCRIPT>

Click here to view the result.

Note that I have used single quotes to enclode the text in write(). Why? Because it is my habit to contain HTML attribute values with double quotes. If I had used double quotes to surround both the attribute value and the text in write(), I would have received an error from the JavaScript interpreter.
This kind of quote nesting is used often with event handlers and it's important that you remember it.

JavaScript allows inclusion of all HTML tags in write(). You can actually build entire HTML documentes starting from <HTML> tag!



Assignments
  1. Using JavaScript, display your name in green color.
  2. Now change the code to display your first name in blue and last name in red.
  3. Using only write, display
    JavaScript is truly
    Amazing.
    
  4. <BOOK> is not an HTML tag and will be ignored by browsers. What do you think will happen if you include this tag in write()?
   Click here for possible answers



Online Workspace

Back Next


AddThis Social Bookmark Button
Page contents: Writing javascript with html - introduction to javascript - javascript on web pages

Page URL: http://www.webdevelopersnotes.com/tutorials/javascript/ javascript_introduction_writing_javascript_web_page_html.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