CNIT 133 - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

CNIT 133

Description:

CNIT 133. 3 ways to run JavaScript. Objectives. Where do I put my JavaScript? JavaScript in URL ... directly call a function defined inside the head element. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 7
Provided by: hy86
Category:
Tags: cnit | definition | url

less

Transcript and Presenter's Notes

Title: CNIT 133


1
CNIT 133
  • 3 ways to run JavaScript

2
Objectives
  • Where do I put my JavaScript?
  • JavaScript in URL
  • JavaScript inside the ltbodygt
  • Call a JavaScript function

3
Where do I put my JavaScript?
  • JavaScript is placed on your HTML page, and must
    be identified with a ltscriptgtlt/scriptgt
  • ltscriptgt
  • lt/scriptgt
  • Often you will see this written as
  • ltscript typetext/javascriptgt
  • lt/scriptgt
  • These script tags generally go in the
    ltheadgtlt/headgt section of your HTML page. The
    reason is that the browser reads this section
    first, and we will want most of our JavaScripts
    available for the entire page.
  • Instead of actually putting the JavaScript into
    the HTML page, inline with the HTML code, you can
    write the code externally in a text file. The
    file should be named with a .js extension. Then
    your page would simply include the external
    code by using tags as follows
  • ltscript srcmyscript.jsgt
  • lt/scriptgt
  • Code written this way has the advantage of being
    reusable. Code that needs to run on multiple
    pages on your site can now be written once and
    included on each page that needs it.

4
JavaScript in URL
  • copy the following JS code and paste it in the
    URL, then hit the enter key. javascript var
    nownew Date() "lth1gt the time is lt/h1gt" now

5
JavaScript inside the ltbodygt
  • Directly code the JS inside the body element.
    (Normally use the document.write() function to
    display the output) Inside the ltbodygt element
    ltscript language"javascript"gt var a 10
    ...... document.write("Resut is " a)
    lt/scriptgt

6
Call a JavaScript function
  • From the body element, directly call a function
    defined inside the head element. (Normally use
    the alert() function to display output) Inside
    the head element (defines the function)ltscript
    language"javascript"gt function f1() var a
    10 ........... alert("Result is " a)
    lt/scriptgt Inside the body element (call the
    function) lta href"javascript f1()"gt Click to
    run JSlt/agt
Write a Comment
User Comments (0)
About PowerShow.com