Home »

How do we get JavaScript onto a web page?

Question ListCategory: JavascriptHow do we get JavaScript onto a web page?
ethanbrown author asked 8 years ago
1 Answers
adamemliy16 author answered 8 years ago

You can use several different methods of placing JavaScript in you pages.

You can directly add a script element inside the body of page.

1. For example, to add the “last updated line” to your pages, In your page text, add the

following:

<p>blah, blah, blah, blah, blah.</p>

<script type=”text/javascript” >

<!– Hiding from old browsers

document.write(“Last Updated:” +

document.lastModified);

document.close();

// –>

</script>

(Note: the first comment, “<–” hides the content of the script from browsers that don’t

understand javascript. The “// –>” finishes the comment. The “//” tells javascript that this

is a comment so JavaScript doesn’t try to interpret the “–>”. If your audience has much

older browsers, you should put this comments inside your JavaScript. If most of your

audience has newer browsers, the comments can be omitted. For brevity, in most examples

here the comments are not shown. )

The above code will look like this on JavaScript enabled browsers,

2.JavaScript can be placed inside the <head> element

Functions and global variables typically reside inside the <head> element.

<head>

<title>Default Test Page</title>

<script language=”JavaScript” type=”text/javascript”>

var myVar = “”;

function timer(){setTimeout(‘restart()’,10);}

document.onload=timer();

</script>

</head>

JavaScript can be referenced from a separate file

JavaScript may also a placed in a separate file on the server and referenced from an HTML

page. (Don’t use the shorthand ending “<script … />). These are typically placed in the

<head> element.

<script type=”text/javascript” SRC=”myStuff.js”></script>
<p>yada, yada, yada.</p>

Please login or Register to Submit Answer