Question: How do we get JavaScript onto a web page?Answer: 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> <p>yada, yada, yada.</p> (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 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> |
Is it helpful?
Yes
No
Most helpful rated by users:
- What are JavaScript data types?
- What\'s relationship between JavaScript and ECMAScript?
- What does isNaN function do?
- How do you convert numbers between different bases in JavaScript?
- What is negative infinity?