Start learning with our library of video tutorials taught by experts. Get started

JavaScript Essential Training

Knowing the JavaScript to avoid


From:

JavaScript Essential Training

with Simon Allardice

Video: Knowing the JavaScript to avoid

JavaScript is not only the most popular programming language in the world, it's one of the most badly written and because of its history, there's a lot of bad JavaScript out there on the web. And there are several things you're likely to see when reading JavaScript that I specifically chose not to include in this course. Now at this point, I'm going to tell you a few of them, so you can recognize them and make your own choice about whether to look deeper at them or not. And the first one is document.write. This is very commonly seen in early JavaScript tutorials.
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
JavaScript Essential Training
5h 31m Beginner Jul 22, 2011

Viewers: in countries Watching now:

Use JavaScript to add new features and a richer, more compelling user interface on web pages. This course keeps current best practices and practical uses for JavaScript in mind, while covering syntax, working with the DOM, and developing and debugging across multiple platforms, devices, and browsers. Author Simon Allardice also shows how to progressively enhance and gracefully degrade web pages, and take advantage of the world of JavaScript libraries now available.

Topics include:
  • Understanding the structure of JavaScript code
  • Creating variables, functions, and loops
  • Writing conditional code
  • Sending messages to the console
  • Working with different variable types and objects
  • Creating and changing DOM objects
  • Event handling
  • Working with timers
  • Debugging JavaScript
  • Building smarter forms
  • Working with CSS, HTML5, and JavaScript
  • Using regular expressions
Subjects:
Developer Web Programming Languages Web Development
Software:
JavaScript
Author:
Simon Allardice

Knowing the JavaScript to avoid

JavaScript is not only the most popular programming language in the world, it's one of the most badly written and because of its history, there's a lot of bad JavaScript out there on the web. And there are several things you're likely to see when reading JavaScript that I specifically chose not to include in this course. Now at this point, I'm going to tell you a few of them, so you can recognize them and make your own choice about whether to look deeper at them or not. And the first one is document.write. This is very commonly seen in early JavaScript tutorials.

It lets you write a string directly to the current page, and as you can see from this example, you can even include HTML tags inside it. Let me show you a basic example here. So I've got a very straightforward HTML page and instead of making a separate JavaScript file, I've just got two document.write calls inside a script tag here. If I run this page, I'll see that this is outputting some text directly into the body of the page, whereas the previous text is just here inside a h1 and the paragraph tag.

And it doesn't look so bad, but there are a few issues with this. See document.write doesn't work in XHTML, it doesn't understand the DOM properly, and it just spits out stuff wherever it's placed. It's a clunky inflexible way of doing anything with a page. And the most substantial reason that you don't want to get used to it is because you can only use it when the page is initially loading. If, for example, I decide to have a function and put document right inside that function and then I'm going to just call that function a couple of seconds after the page has loaded, let's see what happens. So I save that.

I flip over to Firefox where we have it. I refresh this page and two seconds later, the document.write destroys the original page, because it will only work as the page is being created for the first time. Run it anytime after the page is loaded, it will erase the contents of the document, so much better to use innerHTML, or better still, to work with creating and manipulating the individual DOM elements and text nodes. Next step is if you ever see code that looks something like this, browser-sniffing code. You're reading something that seems to be detecting Netscape or Microsoft Internet Explorer or a particular platform.

This is really old-school stuff. Browser sniffing, it used to be necessary, but it hasn't been for several years, and it's usually a dead giveaway that what you're looking at is very old code, or it's written by someone who hasn't brushed up their JavaScript skills in way too long. Any code that seems to be detecting an actual browser or an actual platform should be looked on with great skepticism. While there might be a very occasional edge case where you want to do something with a particular browser, in general, any detection we should do is detecting features, not browsers, and that's much simpler than working with the navigator.userAgent properties.

Next step is the EVAL function. EVAL is a powerful little function that can take an expression and execute it as code. That sounds a little weird, so let me show you what I mean. Let's say I create a variable a that's a string and it contains kind of half a statement here, and variable b contains hello, and variable c contains a closing single quote and closing parentheses. What we can actually do is use EVAL and then we could combine those three variables together into one long string. And if I pass it to EVAL function, EVAL will actually cause that to be executed as if it was code.

Now the issue is there are almost always better ways to do it than EVAL. This is really ugly. It's hard to figure out what's going on. It's inefficient. It can lead to security problems, because you're executing code that's stored in variables and if you're reading one of those variables from the outside world, say the contents of a form field, it can effectively inject code you didn't write into your JavaScript. It's generally agreed upon that EVAL is a bad thing, so you might see it, but don't do it.

And the fourth thing I wanted to mention is what's called a pseudo-protocol. This is very common to see inside HTML. It's a way of making a link or a button directly call a function. So in a regular HTML anchor tag, instead of having an a href equal to the address of a web page using HTTP, we have javascript: and then say the name of a function. This is called a pseudo-protocol, because usually what you'd be doing here is linking to http: or https: and those are real Internet protocols; javascript: is not.

It's just a way of telling the browser to call a JavaScript function here instead of going to an actual address. Okay, it looks pretty easy, but what's the problem? Well, one, you're mixing your HTML with your script, not a good a thing in principle; and two, if JavaScript is disabled, this is useless. You have a link that does nothing. Now there is a similar format that's slightly better, where you use the name of the event. So in this case I do actually have an href here that's pointing to an actual page, an actual address, and then I have onclick=, in this case someFunction; return false.

This is preferable to the pseudo-protocol. Here, at least I have treated this link as the link it's meant to be, with a real address, and I've added the onclick handler as a separate attribute. Now what you often see here is the statement "return false" and this is here to stop the default behavior of this link, which would be to follow the actual link here. So in this case, if JavaScript is enabled, we will run the JavaScript function called someFunction and if not, we would call the JavaScript HTML link and follow that link.

And I'm not a big fan of this method either, and I much prefer leaving my HTML as pure content as possible and adding event handlers directly inside my code, which is why, all along in this course, we've attempted to use JavaScript that when it runs adds functionality to an existing page that already works, since, again, this is the concept of progressive enhancement.

There are currently no FAQs about JavaScript Essential Training.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked