- Overview
- Transcript
4.1 Writing Inline JavaScript or Including External JavaScript
With Jade you can easily incorporate JavaScript by writing it inline or including it from an external file. In this lesson, learn the simple methods for adding JavaScript anywhere.
1.Introduction2 lessons, 11:12
1.1Top-Speed HTML and Easy Templating01:33
1.2Quick and Easy Setup09:39
2.Jade Lang for HTML Shorthand5 lessons, 41:24
2.1Fast HTML Tags and Nesting13:31
2.2Placing Text Inside Tags07:11
2.3Shortcuts to Insert Classes and IDs05:50
2.4Add Full Tag Attributes09:03
2.5Create a Basic Page Structure05:49
3.Jade Lang Templating Basics4 lessons, 38:05
3.1Creating Parent and Child Templates14:34
3.2Append and Prepend Blocks08:07
3.3Use Variables and Conditionals to Configure Templates09:47
3.4Override Config Variables Per Page05:37
4.Incorporating Other Languages4 lessons, 21:50
4.1Writing Inline JavaScript or Including External JavaScript05:33
4.2Using Language Filters07:06
4.3Inline HTML When You Need It03:12
4.4External Markdown/HTML Content via Includes05:59
5.Intermediate Jade Techniques2 lessons, 24:47
5.1Using JavaScript Processes With Jade Variables10:31
5.2Create a Nav Menu With a Mixin and Iteration14:16
6.Conclusion1 lesson, 01:11
6.1Wrapping Up01:11
4.1 Writing Inline JavaScript or Including External JavaScript
Hi, and welcome back to Top Speed HTML Development with Jade. In this lesson you're going to learn how to start incorporating JavaScript into your Jade files, and you'll learn two ways that you can do this. The first is by writing JavaScript inline inside your Jade document, and the other is to include JavaScript from an external site, but have it still compile to become part of your eventual HTML document. We're going to start with learning how to write JavaScript inline, so you don't have to do anything outside of your Jade file for this process. All you need is just one little trick. And you learned in the last lesson that we have the ability, using our config setup, to determine whether or not jQuery loads. Now, in this case, we don't have jQuery loading on this page, and we are using a little code block here to show that there is no jQuery. And currently, it looks like this. Do we have jQuery? Nope, no jQuery. And what we're going to do is write a little bit of inline JavaScript that will check for the presence of jQuery, and if it's found, it will replace the contents of this little block here. Its very, very simple to write JavaScript inline inside any Jade file. As with other HTML elements, you start by just writing out the actual element that you want to have, which is, in this case, the script tag. So just to show you the first part of the process, we'll save and compile that, and now you can see we have our opening and closing script tags. Now we want to include a little JavaScript inside there, and all we have to do is just add a dot at the end of that script tag. And then that will let the system know that the code that's nested after that dot is JavaScript, and should just be treated as JavaScript. So, I'll just copy and paste in a little code. So, this is our JavaScript. We are checking for the presence of jQuery, and if jQuery is available, we will replace the contents of the message here. So, we will change this to true, so we are loading JavaScript, and we'll save and compile. And now you can see we have our JavaScript inline in our HTML file. Now, if we head over to our live site, there we have it. The JavaScripts running successfully, it's checked for the presence of jQuery, and it has replaced the contents of this box with the appropriate message. So, anytime you need to write JavaScript inline in a Jade file, just remember that you add your script element to create your opening and closing script tags, and then you follow it by a dot. And after that you're fine to go ahead and just start writing your JavaScript inline. Sometimes though, you might be using JavaScript that is a little too large, or a little too long, to be writing it directly inside your Jade files, so you might want to pull in a external JavaScript file. For example, this is a little bit of code that I use often to help ensure that iFrame code like YouTube videos and what have you will always respond well with different size windows. It's not large enough that I want to keep it out as a separate JavaScript file. I do want it inline in the HTML document, but it's that little bit too large for me to want to actually write it inline inside the Jade file. So to have all of this code drawn in, and placed inside the HTML file, it's almost the same technique. We still need our script element to house the JavaScript, however, this time we're going to use include. So, write out include keyword, and then all you need, assuming that your file is in the same folder as the Jade file you're working on, you just need to include the file name, which in this case is rspm-iframes.js. Now, if we save and compile, you can see that the JavaScript from our external file has been included, and it's been written directly into the HTML file. Now, for some reason right now, this does not compile if I wrap it with talking marks. At the moment it needs to be written without talking marks, and that may just be a quirk of compilation. But if you find that you do get a compilation error, try using with or without talking marks right there. So, those are the two ways that you can include JavaScript into your Jade files. Again, it's all very, very easy. In the next lesson, you're going to learn how to incorporate other languages into your Jade files using language filters. And language filters will let you write markdown, CoffeeScript, stylelist, and languages like that, also directly inside your Jade files.