|
| 1 | +# Adding Links and Scripts |
| 2 | + |
| 3 | +This site is missing the CSS styles. It's also missing JavaScript code that will convert the URLs ("http://") into links that can be clicked. |
| 4 | + |
| 5 | +Your colleagues have already written the .css and .js files. Complete the following steps to get this site working: |
| 6 | + |
| 7 | +1. Add a `<link>` element that includes the `missing-styles.css` file in this exercise's directory. This should be placed in the `<head>` element. |
| 8 | +2. Add a `<script>` element that includes the `convertUrls.js` file in this exercise's directory. This should be placed at the end of the `<body>` element. |
| 9 | + |
| 10 | +_Hint: When you are done, the messages should have the colors and layout like other exercises, and you should be able to click on the URLs ("http://") to go to them._ |
| 11 | + |
| 12 | +If you are having trouble, review the following information about including resources into an HTML page. |
| 13 | + |
| 14 | +## Separation of Concerns |
| 15 | + |
| 16 | +When building applications for the web, we separate content, presentation and behaviour. |
| 17 | + |
| 18 | +- **Content** is the raw material of the website. It may be an article, a message, a video, or a list of links. |
| 19 | +- **Presentation** is the way that material is displayed. It controls the colors, typography and layout of the content. |
| 20 | +- **Behaviour** is anything that reacts to what the user does. When you type a message on Facebook and hit Send, the website is reacting to your behaviour. |
| 21 | + |
| 22 | +To keep our projects organised, we separate these concerns and store them in different files. The content is written in HTML code and saved in .html files. The presentation is written in CSS code and saved in .css files. The behaviour is written in JavaScript code and saved in .js files. |
| 23 | + |
| 24 | +## Linking a CSS file |
| 25 | + |
| 26 | +HTML allows us to link a CSS file, so that we can apply presentation styles to our raw content. We use the `<link>` tag to do this: |
| 27 | + |
| 28 | +``` |
| 29 | +<link rel="stylesheet" href="style.css"> |
| 30 | +``` |
| 31 | + |
| 32 | +This example has two attributes. The `rel` attribute indicates that our link is to a "stylesheet", a common name for a .css file. The `href` attribute points to the file. |
| 33 | + |
| 34 | +This `<link>` tag goes into the `<head>` element of our HTML page: |
| 35 | + |
| 36 | +``` |
| 37 | +<html> |
| 38 | + <head> |
| 39 | + <title>Example Page</title> |
| 40 | + <link rel="stylesheet" href="style.css"> |
| 41 | + </head> |
| 42 | + <body> |
| 43 | + <p>My page content.</p> |
| 44 | + </body> |
| 45 | +</html> |
| 46 | +``` |
| 47 | + |
| 48 | +## Linking a JavaScript file |
| 49 | + |
| 50 | +HTML allows us to link a JavaScript file, so that we can react when viewers do something on our website. We use the `<script>` tag to do this: |
| 51 | + |
| 52 | +``` |
| 53 | +<script src="postMessage.js"></script> |
| 54 | +``` |
| 55 | + |
| 56 | +This example has one attribute. The `src` attribute points to the file. |
| 57 | + |
| 58 | +Usually, the `<script>` tag appears near the end of the `<body>` element of our HTML page: |
| 59 | + |
| 60 | +``` |
| 61 | +<html> |
| 62 | + <head> |
| 63 | + <title>Example Page</title> |
| 64 | + </head> |
| 65 | + <body> |
| 66 | + <p>All of my page content.</p> |
| 67 | + <script src="postMessage.js"></script> |
| 68 | + </body> |
| 69 | +</html> |
| 70 | +``` |
| 71 | + |
| 72 | +## Linking to the right file location |
| 73 | + |
| 74 | +When linking a CSS or JavaScript file, you must provide the correct path to the file. In this exercise, `missing-styles.css` is placed inside of a directory called `css`. Your `<link>` must point to the file inside of that directory: |
| 75 | + |
| 76 | +``` |
| 77 | +<link rel="stylesheet" href="css/missing-styles.css"> |
| 78 | +``` |
| 79 | + |
| 80 | +Want to learn more? Read about [Relative Links](https://marksheet.io/html-links.html#relative-urls), [Absolute Links](https://marksheet.io/html-links.html#absolute-urls), and [how to choose the right one](https://marksheet.io/html-links.html#relative-or-absolute-links). |
0 commit comments